MongoDB Study Notes (2)-add, delete, modify, and query

Source: Internet
Author: User
Tags findone
Query records

Common Query

> Var cursor = dB. Things. Find ();

> While (cursor. hasnext () printjson (cursor. Next ());
The above example shows the cursor-style iterative output. The hasnext () function tells us whether there is any data. If so, you can call the next () function.

When we use JavaScript shell, we can use the JS features, and foreach can output the cursor. the following example uses foreach () for loop output: foreach () must define a function for each cursor element to call.

> DB. Things. Find (). foreach (printjson );

In MongoDB shell, we can also use the cursor as an array:
> Var cursor = dB. Things. Find ();

> Printjson (cursor [4]);

 

When using a cursor, pay attention to the memory usage, especially for large cursor objects, which may be output through iteration. the following example converts a cursor to a real array type:

> Var arr = dB. Things. Find (). toarray ();

> Arr [5];
Conditional Query

> DB. Things. Find ({name: "mongo"}). foreach (printjson );

> DB. Things. Find ({X: 4, Y: "ABC"}). foreach (printjson );

Returns a specific element.

> DB. Things. Find ({X: 4}, {J: true}). foreach (printjson );

Findone () syntax

Printjson (db. Things. findone ({name: "mongo "}));

Limit the number of result sets by using limit
> DB. Things. Find (). Limit (3 );

 

Modify record

 

> DB. Things. Update ({name: "mongo" },{$ set: {name: "mongo_new "}});

Delete record

> DB. Things. Remove ({name: "mongo_new "});

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.