MongoDB insert query data

Source: Internet
Author: User
Insert: usekeke automatically generates the keke database after data is written. J {name: mongo };{ name: mongo} t {x: 3 };{ x: 3} db. things. save (j); db. things. save (t); query db. things. find (); {_ id: ObjectId (4c2209f9f3924d31102bd84a), name: m

Insert: use keke automatically generates the keke database after data is written. J = {name: "mongo" };{ "name": "mongo"} t = {x: 3 };{ "x": 3} db. things. save (j); db. things. save (t); query db. things. find (); {"_ id": ObjectId ("4c2209f9f3924d31102bd84a"), "name": "m

Insert:

Use keke

The keke database is automatically generated after data is written.

> J = {name: "mongo" };{ "name": "mongo" }> t = {x: 3 };{ "x": 3}> db. things. save (j);> db. things. save (t );

Query

> Db. things. find (); {"_ id": ObjectId ("4c2209f3924d31102bd84a"), "name": "mongo"} {"_ id": ObjectId ("4c2209fef3924d31102bd84b "), "x": 3}

> Var cursor = db. things. find ();> while (cursor. hasNext () printjson (cursor. next (); {"_ id": ObjectId ("501c7e5cab2f3c46faf8e68a"), "name": "mongo"} {"_ id": ObjectId ("objective "), "x": 3}

ForEach () must define a function for each cursor element to call. forEavh is case sensitive, Hong Kong Space, javascript is case sensitive.

> Db. things. find (). forEach (printjson); {"_ id": ObjectId ("501c7e5cab2f3c46faf8e68a"), "name": "mongo"} {"_ id": ObjectId ("external "), "x": 3}

We can also use the cursor as an array:

> Var cursor = db. things. find ();> printjson (cursor [1]); {"_ id": ObjectId ("501c7e65ab2f3c46faf8e68b"), "x": 3}>

The following example converts a cursor to a real array type:

> Var arr = db. things. find (). toArray ();> arr [1] {"_ id": ObjectId ("501c7e65ab2f3c46faf8e68b"), "x": 3}>

, U.S. server, Hong Kong server rent

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.