MongoDB Foundation (Add, delete, change, check) operation

Source: Internet
Author: User
Tags bulk insert

First, insert

The INSERT statement for MongoDB is in this format:

db.collection.insert(document)

Where documents are document data, collection is a collection of document data. If collection exists, the document is added to the collection directory, and if collection does not exist, the database creates collection before saving the document.
For example: Save name and age to the person collection

db.person.insert({name:"八豆少爷",age:100})

The above is a single insert, the bulk Insert is similar, when inserting multiple documents, the insert command parameter is an array, the array element is a Bson format document, multiple documents can be placed in an array, one time to insert multiple data,
For example: Db.users.insert ([{name: "Eight Beans", Age:1000},{name: "id farmer", age:1}])
However, there are also some issues to be aware of when using bulk inserts, because the Bson format limits the amount of data that can be inserted at one time, and when multiple data is inserted into an insert command, MONGODB does not guarantee full success or failure altogether.
To view the inserted person document, you can use: Db.person.find () to view the data in the person collection in the current library.
Second, query
Use the Find command when querying data in MongoDB using the following method:

Grammar:

db.collection.find(criteria,projection);

Parameters:
criteria– query criteria, document type, optional.
projection– returns the field, document type, optional, if you want to return all fields, this parameter is ignored.
The Find command has two optional arguments, criteria is the query condition, projection is the returned field, and if not passed in the condition database returns all documents for that collection.
For example, query out all the data in the person collection.

db.person.find()

To view a list of collections in the current database, you can use: Show collections.
Third, update
The update command can update specific field values for the specified document or replace the entire document, and MongoDB will reallocate space and relocate if the update operation increases the document size.
Grammar:

db.collection.update(query,update,upsert:boolean,multi:boolean});

Parameters:
Query: Search criteria, documents, and query conditions in find are written in the same notation.
Update: Modify the content, document.
Upsert (optional): If the value is true, the document is created when there are no matching documents in the collection. False by default.
multi (optional): If the value is true, all documents that match the criteria are updated, otherwise only one document is updated, false by default.
Here's an example of updating all data in the person collection that is less than 30 years old, to update their status to ' X '.

db.person.update({age:{$lt:30}},{$set:{status:"X"}},{multi:true})

Note: The less-than operator is LT,Biginof theDamnmadecharacteris a Gt

The Save command can update or insert a new document, unlike the Update command, where save can operate on only one document.
Grammar:

db.collection.save();

Parameters:
Document: New documents;

For example: Use the Save command to save the document {name: "Tony", Age:12,gender: "Man"} to the person collection

db.person.save({name:"Tony",age:12,gender:"man"})

Four: Delete
When you need to delete a document using the Remove command, deleting the document cleans up unwanted data, frees up storage space, and improves retrieval efficiency, but deleting the error can be a disaster, so you need to be very cautious in performing data deletion operations!
Grammar:
Db.collection.remove (
Query
Justone
)
Parameters:
The Query:bson type, which removes the condition of the document.
Justone: Boolean type, true: Delete Only one document, false: default, delete all documents that match the criteria.
The following is a document deletion of all status= "X" in the Users collection, comparing the operations of MongoDB and traditional SQL database deletions to see what is different.
Compare to traditional SQL-delete document
Mongodb

db.person.remove({status:"X"})

Traditional SQL

delete from person where status = ‘X‘

MongoDB Foundation (Add, delete, change, check) operation

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.