MongoDB data creation method and query example

Source: Internet
Author: User
Tags findone mongodb

1. Connecting to the database

[Root@localhost ~]#/apps/mongo/bin/mongo
MongoDB Shell version:1.8.1
Connecting To:test
>

2. Insert Record

>j = {name: ' MONGO '};
{' name ': ' MONGO '}
> t = {x:3};
{"X": 3}
> Db.things.save (j);
> Db.things.save (t);
> Db.things.find ();
{"_id": ObjectId ("4c2209f9f3924d31102bd84a"), "name": "MONGO"}
{"_id": ObjectId ("4c2209fef3924d31102bd84b"), "X": 3}
>

> for (var i = 1; i < i++) Db.things.save ({x:4, j:i}); > Db.things.find ();
{"Name": "Mongo", "_id": ObjectId ("497CF60751712CF7758FBDBB")}
{"X": 3, "_id": ObjectId ("497cf61651712cf7758fbdbc")}
{"X": 4, "J": 1, "_id": ObjectId ("497CF87151712CF7758FBDBD")}
{"X": 4, "J": 2, "_id": ObjectId ("497cf87151712cf7758fbdbe")}
{"X": 4, "J": 3, "_id": ObjectId ("497cf87151712cf7758fbdbf")}
{"X": 4, "J": 4, "_id": ObjectId ("497cf87151712cf7758fbdc0")}
{"X": 4, "J": 5, "_id": ObjectId ("497CF87151712CF7758FBDC1")}
{"X": 4, "J": 6, "_id": ObjectId ("497cf87151712cf7758fbdc2")}
{"X": 4, "J": 7, "_id": ObjectId ("497CF87151712CF7758FBDC3")}
{"X": 4, "J": 8, "_id": ObjectId ("497CF87151712CF7758FBDC4")}

3. Query records

3.1 General Enquiries

>var cursor = Db.things.find ();
> while (Cursor.hasnext ()) Printjson (Cursor.next ());
{"_id": ObjectId ("4c2209f9f3924d31102bd84a"), "name": "MONGO"}
{"_id": ObjectId ("4c2209fef3924d31102bd84b"), "X": 3}
{"_id": ObjectId ("4c220a42f3924d31102bd856"), "X": 4, "J": 1}
{"_id": ObjectId ("4c220a42f3924d31102bd857"), "X": 4, "J": 2}
{"_id": ObjectId ("4c220a42f3924d31102bd858"), "X": 4, "J": 3}
{"_id": ObjectId ("4c220a42f3924d31102bd859"), "X": 4, "J": 4}
{"_id": ObjectId ("4c220a42f3924d31102bd85a"), "X": 4, "J": 5}

The above example shows the iterative output of the cursor style. The Hasnext () function tells us if there is data, and if so
You can call the next () function.

When we use the JavaScript shell, we can use the characteristics of JS, ForEach can output the cursor. The following example
The child is using foreach () to loop the output: foreach () must define a function for each cursor element to invoke.

> Db.things.find (). ForEach (Printjson);
{"_id": ObjectId ("4c2209f9f3924d31102bd84a"), "name": "MONGO"}
{"_id": ObjectId ("4c2209fef3924d31102bd84b"), "X": 3}
{"_id": ObjectId ("4c220a42f3924d31102bd856"), "X": 4, "J": 1} ...

In the MongoDB shell, we can also use cursors as arrays:

> var cursor = db.things.find ();
> Printjson (cursor[4]);
{"_id": ObjectId ("4c220a42f3924d31102bd858"), "X": 4, "J": 3}

When using cursors, be aware of memory-consuming problems, especially large cursor objects, which may overflow memory. So should
The iteration is the way to output. The following example converts a cursor to a true array type:

> var arr = db.things.find (). ToArray ();
>arr[5];
{"_id": ObjectId ("4c220a42f3924d31102bd859"), "X": 4, "J": 4}

3.2 Piece Query

The following example shows how to execute a SQL-like query and demonstrates how to implement it in MongoDB. This
Query in the MongoDB shell, of course, you can also use other application-driven or language to achieve:

SELECT * from things WHERE name= "MONGO"
> Db.things.find ({name: "MONGO"}). ForEach (Printjson);
{"_id": ObjectId ("4c2209f9f3924d31102bd84a"), "name": "MONGO"}

SELECT * from things WHERE x=4
>db.things.find ({x:4}). ForEach (Printjson);
{"_id": ObjectId ("4c220a42f3924d31102bd856"), "X": 4, "J": 1}
{"_id": ObjectId ("4c220a42f3924d31102bd857"), "X": 4, "J": 2}
{"_id": ObjectId ("4c220a42f3924d31102bd858"), "X": 4, "J": 3}
{"_id": ObjectId ("4c220a42f3924d31102bd859"), "X": 4, "J": 4}
{"_id": ObjectId ("4c220a42f3924d31102bd85a"), "X": 4, "J": 5}

The query condition is {a:a, b:b, ...} Similar to "where A==a and B==b and ...". The above shows all the elements, and of course we can return a particular element, similar to the value of a field in the return table.
Only need to specify the name of the element in find ({x:4})

SELECT J from Things WHERE x=4
> Db.things.find ({x:4}, {j:true}). ForEach (Printjson);
{"_id": ObjectId ("4c220a42f3924d31102bd856"), "J": 1}
{"_id": ObjectId ("4c220a42f3924d31102bd857"), "J": 2}
{"_id": ObjectId ("4c220a42f3924d31102bd858"), "J": 3}
{"_id": ObjectId ("4c220a42f3924d31102bd859"), "J": 4}
{"_id": ObjectId ("4c220a42f3924d31102bd85a"), "J": 5}

The item in the query table that has a field:

Db.things.find ({},{"* * *": true})//*** for the lookup field

3.3findOne syntax

To facilitate consideration, the MongoDB shell provides a findone () function to avoid the overhead that a cursor might incur. This letter
The number is the same as the Find () function, but it returns the first data in the cursor, or returns NULL, the null data.

As an example, name= "MONGO" can be implemented in a number of ways, using next () to loop the cursor or when
Do the array to return the first element.

But using the FindOne () method is simpler and more efficient:

> Printjson (Db.things.findOne ({name: "MONGO"}));
{"_id": ObjectId ("4c2209f9f3924d31102bd84a"), "name": "MONGO"}

3.4 Limit the number of result sets by limit

If you need to limit the length of the result set, you can call the Limit method.
This is a highly recommended way to address performance problems by limiting the number of bars to network transfers, such as:

> Db.things.find (). limit (3);
{"_id": ObjectId ("4c2209f9f3924d31102bd84a"), "name": "MONGO"}
{"_id": ObjectId ("4c2209fef3924d31102bd84b"), "X": 3}
{"_id": ObjectId ("4c220a42f3924d31102bd856"), "X": 4, "J": 1}

4. Modify the Record

Modify the name of the record that name is MONGO to Mongo_new

> db.things.update ({name: "MONGO"},{$set: {name: "Mongo_new"}});

Let's check to see if we've changed.

> Db.things.find ();
{"_id": ObjectId ("4faa9e7dedd27e6d86d86371"), "X": 3}
{"_id": ObjectId ("4faa9e7bedd27e6d86d86370"), "name": "Mongo_new"}

5. Delete records

Delete the record of user name is mongo_new from the collection things

>db.things.remove ({name: "Mongo_new"});
> Db.things.find ();
{"_id": ObjectId ("4faa9e7dedd27e6d86d86371"), "X": 3}

It was verified that the record was actually deleted.

6. Memory Release

>use Admin

>db.runcommand ({closealldatabases:1})

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.