MongoDB journey (2) basic operations (MongoDBJavascriptShell)

Source: Internet
Author: User
Tags mongodb update
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 ();

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.