MONGODB basic Operations-database collection document additions and deletions

Source: Internet
Author: User
Tags mongodb query

Database operations:

// see which databases are available

> Show DBS

Local 0.078GB

MyDB 0.078GB

//use operation will switch to a database If the database exists, it will switch directly If it does not exist then after the switchover, the first time you perform a new modification operation to create this database

> Use newdb

Switched to DB newdb

// This is not yet created

> Show DBS

Local 0.078GB

MyDB 0.078GB

> Db.persion.insert ({age:10})

Writeresult ({"ninserted": 1})

// database has been created

> Show DBS

Local 0.078GB

MyDB 0.078GB

Newdb 0.078GB

// Deleting a database

> Db.dropdatabase ()

{"Dropped": "newdb", "OK": 1}

> Show DBS

Local 0.078GB

MyDB 0.078GB

Collection operations:

> Use MyDB

Switched to DB MyDB

// View all collections of the current database empty because it's a new database

> Show Tables

// Insert a piece of data This will automatically insert a collection of person

> Db.person.insert ({name: ' ZJF ', age:30})

Writeresult ({"ninserted": 1})

// View All Collections already have a person (system.indexes is System-generated)

> Show Tables

Person

System.indexes

// Delete Collection

> Db.person.drop ()

True

> Show Tables

System.indexes

Document Insertion

// using Db.collection.insert () Grammar

> Db.person.insert ({name: ' ZJF ', age:30})

Writeresult ({"ninserted": 1})

> Db.person.find ()

{"_id": ObjectId ("592ffd872108e8e79ea902b0"), "name": "ZJF", "Age": 30}

Document Updates update () method

The update () method is used to update a document that already exists. The syntax format is as follows:

Db.collection.update (
<query>,
<update>,
{
Upsert: <boolean>
Multi: <boolean>
Writeconcern: <document>
}
)

Parameter description:

    • Query : The query condition for update, similar to where in SQL update query.
    • Update : Update objects and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
    • Upsert : Optional, this parameter means that if there is no record of update, insert Objnew,true is inserted, default is False, do not insert.
    • Multi : Optional, mongodb default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all.
    • Writeconcern : Optional, throws an exception level.

> Db.person.insert ({name: ' Xhj ', age:30})

Writeresult ({"ninserted": 1})

> Db.person.insert ({name: ' Zzj ', age:30})

Writeresult ({"ninserted": 1})

> Db.person.find ()

{"_id": ObjectId ("592ffd872108e8e79ea902b0"), "name": "ZJF", "Age": 30}

{"_id": ObjectId ("592ffe972108e8e79ea902b1"), "name": "Xhj", "Age": 30}

{"_id": ObjectId ("592ffe972108e8e79ea902b2"), "name": "Zzj", "Age": 30}

// Update Age for the (It's been another year)

> db.person.update ({age:30},{$set: {age:31}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

// because the default parameter multi to False so we've only updated one

> Db.person.find ()

{"_id": ObjectId ("592ffd872108e8e79ea902b0"), "name": "ZJF", "Age": 31}

{"_id": ObjectId ("592ffe972108e8e79ea902b1"), "name": "Xhj", "Age": 30}

{"_id": ObjectId ("592ffe972108e8e79ea902b2"), "name": "Zzj", "Age": 30}

// plus multi:true . This is similar to the update for a relational database the

> db.person.update ({age:30},{$set: {age:31}},{multi:true})

Writeresult ({"nmatched": 2, "nupserted": 0, "nmodified": 2})

> Db.person.find ()

{"_id": ObjectId ("592ffd872108e8e79ea902b0"), "name": "ZJF", "Age": 31}

{"_id": ObjectId ("592ffe972108e8e79ea902b1"), "name": "Xhj", "Age": 31}

{"_id": ObjectId ("592fff812108e8e79ea902b2"), "name": "Zzj", "Age": 31}

// save based on a single record to update It's time to have _id . as the primary key Add a separate save when updating a single piece of data Method instead of using update ({"_id": ObjectId ("592ffd872108e8e79ea902b0")}) this way. is to treat a single piece of data as an object.

> Db.person.save ({"_id": ObjectId ("592ffd872108e8e79ea902b0"), "name": "ZJF", "Age": 30})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.person.find ()

{"_id": ObjectId ("592ffd872108e8e79ea902b0"), "name": "ZJF", "Age": 30}

{"_id": ObjectId ("592ffe972108e8e79ea902b1"), "name": "Xhj", "Age": 31}

{"_id": ObjectId ("592fff812108e8e79ea902b2"), "name": "Zzj", "Age": 31}

Document deletion

The basic syntax format for the Remove () method is as follows:

Db.collection.remove (

<query>,

<justOne>

)

If your MongoDB is after the 2.6 version, the syntax format is as follows:

Db.collection.remove (

<query>,

{

Justone: <boolean>

Writeconcern: <document>

}

)

Parameter description:

    • query : (optional) The condition of the deleted document.
    • justone : (optional) If set to TRUE or 1, only one document is deleted.
    • Writeconcern : (optional) the level at which the exception is thrown.

> Db.person.find ()

{"_id": ObjectId ("592ffd872108e8e79ea902b0"), "name": "ZJF", "Age": 30}

{"_id": ObjectId ("592ffe972108e8e79ea902b1"), "name": "Xhj", "Age": 31}

{"_id": ObjectId ("592fff812108e8e79ea902b2"), "name": "Zzj", "Age": 31}

> Db.person.remove ({age:31})

Writeresult ({"Nremoved": 2})

> Db.person.find ()

{"_id": ObjectId ("592ffd872108e8e79ea902b0"), "name": "ZJF", "Age": 30}

Document Query

The syntax format for MongoDB query data is as follows:

Db.collection.find (query, projection)

    • query: Optional, use the query operator to specify the criteria
    • projection : Optional, use the projection operator to specify the returned key. Returns all the key values in the document when queried, just omit the argument (omitted by default).

If you need to read the data in an easy-to-read way, you can use the pretty () method, which has the following syntax:

>db.col.find (). Pretty ()

> Db.person.find ({name: ' ZJF '})

{"_id": ObjectId ("592ffd872108e8e79ea902b0"), "name": "ZJF", "Age": 30}

MONGODB basic Operations-database collection document additions and deletions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.