Common crud operation statements of mongodb


For the back-end God (Xiaobai), the daily work is crud, plus control + C and control + V. As a great God (Xiaobai), how can you not understand crud? Compared with cumbersome SQL statements, mongodb crud is very simple and modern.

Create data (create)

Mongodb provides two ways to create data:

db. crud. Insert ({Name: 'code farmer', gender: 'male'});
db. crud. Save ({Name: 'code farmer on the island', gender: 'male'});

The difference of the save method is that if you carry_ If the ID attribute is used, the corresponding data will be updated. Otherwise, new data will be inserted. After mongodb 3.2, two insertion methods have been added: insertone and insertmany, and the insert method has been marked as obsolete.

db. crud. Insertone ({Name: 'code farmer', gender: 'male'});
db. crud. Insertmany ([{Name: 'code farmer on the island', gender: 'male'}, {Name: 'program Yuan', gender: 'female'}]);

Update data (update)

When updating, the query matching criteria are in the front and the data to be updated are in the back.

#Give a code
db. crud. Update ({Name: 'code farmer'}, {Name: 'code farmer', gender: 'female'});

By default, the update method is to find a matching data update instead of updating all the data. If you need to update multiple data, you need to add the attribute multi: true later. At the same time, it should be noted that all documents will be replaced by new data.

#Give all the codes to me
db. crud. Update ({Name: 'code farmer'}, {Name: 'code farmer', gender: 'female'}, {multi: true});

After mongodb version 3.2, updateone and updatemany methods are added to update one and more data respectively.

#Restore the gender of coder farmers
db. crud. Updateone ({Name: 'code farmer'}, {$set: {Name: 'code farmer on the island', gender: 'male'}});
db. crud. Updatemany ({Name: 'code farmer'}, {$set: {Name: 'code farmer on the island', gender: 'male'}});

In the new version of mongodb, updateone and updatemany are required to be atomic operations, that is, you must specify to use $set to specify the updated fields to prevent misoperation from overwriting the whole document. If it is not specified, an error will be reported: the update operation document must contain atomic operators** It is recommended to use * * * * updateone * * and * * updatemany * * for updates, which are safer and clearer. If the document needs to be replaced, you can use replaceone:

db. crud. Replaceone ({Name: 'code farmer on the island'}, {Name: 'program Yuan', gender: 'female'});


The deletion methods after mongodb version 3.2 are deleteone and deletemany, and one or more matching data are deleted correspondingly.

db. crud. Deleteone ({Name: 'program'});
db. crud. Deletemany ({gender: 'female'});

In earlier versions, the remove method was used. If the second parameter is true, only one matching piece of data will be deleted..

db. crud. Remove ({Name: 'program'});
db. crud. Remove ({gender: 'female'}, true);

It should be noted that if the query parameter object of the remove method is empty, all data will be deleted, which will delete the rhythm of library running.

#Operate carefully and be careful not to delete the database and run away

Read data (read)

The data is read using the find or findone method, where find will return all results. Of course, you can also use limit to limit the number of returned results.

#Query all data
#Only 2 pieces of data are returned
#Query data named Tom
db.crud.find({name: 'Tom'});

If you need to beautify the returned results, you can use the pretty () method.


If you want to return some fields, you can specify the returned fields later if you want to exclude them_ ID needs to be displayed and specified, and other fields are not included. Otherwise, an error will be reported: cannot do exclusion on field gender in inclusion projection.

#Return only_ ID and name fields
db.crud.find({name: 'Tom'}, {name: 1});
#Do not return_ id
db.crud.find({name: 'Tom'}, {_id: 0, name: 1});

The above is the details of the commonly used crud statements of mongodb. For more information about mongodb crud statements, please pay attention to other relevant articles of developeppaer!

Recommended Today

A front-end developer's Vim is the same as an IDE

Here is my new configurationjaywcjlove/vim-webI've been grinding it, and it's basically ready to use. Take it out and cheat the star Install The latest version of Vim 7.4+ uses (brew install macvim) installation, vim version updatebrew install macvim –override-system-vim View configuration locations # Enter vim and enter the following characters :echo $MYVIMRC download vim-web Download […]