MongoDB Finishing Note のcrud

Source: Internet
Author: User
Tags findone mongodb collection

Add to

Let's create a set of test and write some data. Create two objects J and T, and save to the collection. In the example ">" to indicate a shell input prompt
> 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}
>
There are a few points to note:
1. You do not need to create a collection beforehand. It is created automatically the first time the data is inserted.
2, in the document can actually store any structure of data, of course, in the actual application we store or the same type of document collection. This feature can actually be very flexible in the application, you do not need to like ALTER TABLE statement to modify your data structure.
3, each time the data inserted in the collection will have an ID, called _id, will be introduced in detail later!
Here's a bit more data (loop Add):
> for (var i = 1; i < ten; 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")}
Please note that the number of loops here is 10, but only the 8th is shown, and 2 data are not displayed. If you want to continue querying the following data only by using the "it" command, the following data will continue to appear:
{"_id": ObjectId ("4c220a42f3924d31102bd866"), "X": 4, "J": 17}
{"_id": ObjectId ("4c220a42f3924d31102bd867"), "X": 4, "J": 18}
Have more
> It
{"_id": ObjectId ("4c220a42f3924d31102bd868"), "X": 4, "J": 19}
{"_id": ObjectId ("4c220a42f3924d31102bd869"), "X": 4, "J": 20}
Technically find () returns a Cursor object. But in the above example, there is no variable for a cursor. So the shell automatically iterates through the cursor, returns an initialized set, and allows us to continue with the IT iteration output. Of course, we can also directly use the cursor to output, but this is the "cursor" part of the content.
_id Key
In the data types that MongoDB supports, _id is its own product, and the following is a brief introduction to it. Each document stored in the MongoDB collection has a default primary key _id, which is fixed and can be any data type supported by MongoDB, which is objectid by default. In relational database schema design, the primary key is mostly numeric, such as the commonly used int and long, and more commonly, the value of the primary key is obtained from the database, and the order of the primary key value sometimes indicates some logic. In contrast to MongoDB, it is located in the distributed storage system at the beginning of the design, so it does not support the self-increment primary key.
_id Key examples illustrate:
When we write a document to a collection, the system automatically generates a key named _id. For example:
> Db.c1.find ()
{"_id": ObjectId ("4fb5faaf6d0f9d8ea3fc91a8"), "name": "Tony", "Age": 20}
{"_id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "Age": 10}
There is an extra type of Objectid key, which is not specified at the time of insertion, which is somewhat similar to Oracle's ROWID information, which is automatically generated. In MongoDB, each collection must have a field called _id, the field type is Objectid by default, in other words, the field type may not be objectid, for example:
> Db.c1.find ()
{"_id": ObjectId ("4fb5faaf6d0f9d8ea3fc91a8"), "name": "Tony", "Age": 20}
{"_id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "Age": 10}
{"_id": 3, "name": "Bill", "Age": 55}
Although the type of _id can be freely specified, it must be unique in the same set, and if a duplicate value is inserted, the system throws an exception, as follows:
> Db.c1.insert ({_id:3, Name: "Bill_new", age:55})
E11000 duplicate key error index:test.c1.$_id_ dup key: {: 3.0}

>

Enquiry
General Query
Before we go into the query, let's look at how to return a cursor object from a query. You can simply query through find (), and he returns a collection of arbitrary structures. If the implementation of a specific query is explained later.
Implement the same query above and then output through the while:
> 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 example above shows the iterative output of the cursor style. The Hasnext () function tells us if there is any data, and if so, you can call the next () function.
When we use JavaScript shell, we can use the JS feature, and ForEach can output the cursor. The following example uses 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}
{"_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}
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-intensive issues, especially large cursor objects, which can potentially overflow memory. So it should be output in an iterative way. The following example converts a cursor to a real array type:
> var arr = db.things.find (). ToArray ();
> Arr[5];
{"_id": ObjectId ("4c220a42f3924d31102bd859"), "X": 4, "J": 4}
Note that these features are only used in the MongoDB shell, and not all other application drivers are supported. The MongoDB cursor object is not without a snapshot, and if there are other users in the collection for the first time or the last call to next (), you may not get the data in the cursor. So make a clear lock on the cursor you want to query.

Conditional Query
Here we already know how to implement a query from the cursor and return the data object, let's see how to query according to the specified conditions.
The following example shows how to execute a SQL-like query and demonstrates how to implement it in MongoDB. This is in the MongoDB shell query, 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 ...".
All of the elements are shown above, and of course we can return a specific element, similar to the value of returning a field in the table, just 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}
For ease of consideration, the MongoDB shell avoids the potential overhead of cursors and provides a findone () function. This function is the same as the Find () function, but it returns the first data in the cursor, or returns NULL, or empty data. As an example, name= "MONGO" can be implemented in many ways, using next () to loop a cursor or as an 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"}
If you need to limit the length of the result set, you can call the Limit method. This is a highly recommended way to resolve performance problems by limiting the number of bars to reduce network transmission, for example:

> Db.things.find (). limit (3);

{"_id": ObjectId ("4c2209f9f3924d31102bd84a"), "name": "MONGO"}

{"_id": ObjectId ("4c2209fef3924d31102bd84b"), "X": 3}

{"_id": ObjectId ("4c220a42f3924d31102bd856"), "X": 4, "J": 1}

Modify

Change the name of the record name to 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"}

Delete
Deletes a record of user name 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 indeed deleted.
At this point, adding and deleting the case are almost complete, or if there is incomplete, welcome to leave a message!

MongoDB Finishing Note のcrud

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.