Novice MongoDB Learning---(iii) MONGODB Add, delete, change, check (insert, remove, Update, find)

Source: Internet
Author: User
Tags mongodb add

inserting insert

The data structure of the MongoDB document is basically the same as JSON. All data stored in the collection is in the Bson format. Bson is a binary form of a class JSON storage format, referred to as binary JSON.

First we select the database


Let's define a document first

Then we go into today's topic, insert a document into the collection, insert it in two ways, one is to insert a defined document, and the other is to insert undefined data.

> Db.new.insert (document) Writeresult ({"ninserted": 1})

> Db.new.insert ({... "Name": "Jingdong",... "Age": ",..." "Profession": "Extreme Programming",... "City": "Sy" ...} ... ) Writeresult ({"ninserted": 1})

We can then use the Find function to view the documents in the collection.

Db.new.find ()

Delete Remove

The MongoDB remove () function is used to remove data from the collection.

The update () function can be used for MONGODB data updates. It is a good practice to execute the Find () command before executing the Remove () function to determine if the condition of execution is correct.

If you want to delete the data named "Jingdong" in the new collection, you can execute the following command

> Db.new.remove ({"Name": "Jingdong"}) Writeresult ({"Nremoved": 2})

If you want to delete all the data from the new collection, you can execute the following command

Db.new.remove ({})

If you want to delete the entire collection you need to use the drop function, the drop function returns TRUE or FALSE.

Db.new.drop ()

Using the Dropdatabase () function to delete a database, it is best to use DB to view the current database before executing the command, making sure that the database you are deleting is correct.

Db.dropdatabase ()

Updating update

The update () function can be used for MONGODB data updates.

Db.collection.update (criteria, objnew, Upsert, Multi)

The update () function accepts the following four parameters:

    • criteria : The query condition for update, similar to where in SQL update query.
    • objnew : Update object and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
    • Upsert : This parameter means that if there is no record of update, insert Objnew,true is inserted, default is False, not inserted.
    • Multi : 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.
Here are a few small examples, first insert two test data.

> Db.new.insert ({"Name": "Jingdong", "Age": "All", "profession": "Extreme Programming", "City": "Sy"}) Writeresult ({"ninserted": 1}) > Db.new.insert ({"Name": "Nannan", "Age": "All", "profession": "Calc", "City": "Sy"}) Writere Sult ({"ninserted": 1})

Update only the first rule that meets the criteria

> db.new.update ({"City": "Sy"},{$set: {"City": "BJ"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

All updates

> db.new.update ({"City": "Sy"},{$set: {"City": "BJ"}},false,true)

If you do not have this property, the first one is added

> db.new.update ({"City": "Sy"},{$set: {"City": "BJ"}},true,false)

If you do not have this attribute, all are added

> db.new.update ({"City": "Sy"},{$set: {"City": "BJ"}},true,true)

Query find

You can use find to implement all queries

> Db.new.find ()

You can also specify conditional queries

> Db.new.find ({"Name": "Jingdong"})









Novice MongoDB Learning---(iii) MONGODB Add, delete, change, check (insert, remove, Update, find)

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.