MongoDB journey (ii) basic operations (MongoDBJavascriptShell) Postedon after learning about MongoDB, here is a brief introduction to some simple operations on MongoDB. MongoDB Installation Introduction: MongoDBjavascriptshell is a javascript-based interpreter, so it supports js programs. For (i0; i200000
MongoDB journey (ii) basic operations (MongoDB Javascript Shell) Posted on after learning about MongoDB, here is a brief introduction to some simple operations on MongoDB. MongoDB Installation Introduction: MongoDB javascript shell is a javascript-based interpreter, so it supports js programs. For (I = 0; i200000
MongoDB journey (ii) basic operations (MongoDB Javascript Shell) Posted on
After learning about MongoDB, I will briefly introduce some simple operations on MongoDB.
MongoDB installation:
MongoDB javascript shell is a javascript-based interpreter and a website space. Therefore, it supports javascript programs. As follows:
For (I = 0; I <200000; I ++ ){
Db. numbers. save ({num: I });
}
------------------- The following describes some simple operations ---------------------------------------
Use tutorial;
Db. users. insert ({username: "smith"}); db. users. save ({username: "jone "});
Db. users. find (); db. uers. find ({username: "smith "});
Db. users. count ();
Db. users. update ({username: "smith" },{$ set: {country: "Canada "}});
Db. users. update ({username: "smith" },{$ unset: {country: 1 }});
Db. users. update ({username: "smith"}, {$ set: {favorites: {city: ["Beijing", "Chicago"], movies: ["American Beauty ", "..... "] }});
Db. users. find ({"favorites. movies": "American Beauty "});
Add a new element to favorites. movies as follows: $ push, $ addToSet, website space, and virtual host. The latter is unique.
Db. users. find ({username: "smith"}, {$ addToSet: {"favorites. movies": "The Maltess Falcon" }}, false, true );
Parameter 1: Query condition; parameter 2: add element; parameter 4: multi-update ); by default, MongoDB update operations are only applied to the first document matched by the query selector. If you want the operation to be applied to all matched documents, you need to display the description.
Db. foo. remove (); delete the data in the document. The foo file itself will not be deleted, for example, the delete/truncate tablem command;
Db. users. drop (); Delete the document.
Bytes ------------------------------------------------------------------------------------------------
Others:
· Db. members. find ({num: {"$ gt": 1999995 }});
Db. members. find ({num: {"$ gt": 20, "$ lt": 25 }});
· Db. members. find ({num: {"$ gt": 1999995 }}). explain (); [BasicCursor: No index is used; n: number of results; nscanned: Number of scans]
· The ascending index of num is as follows: db. members. ensureIndex ({num: 1}); View index creation: db. members. getIndexes ();