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)