In addition to the tangle of queries, the rest are relatively simple! In fact, when I learned how to add, delete, modify, and query mongodb command lines, I also tried it one by one based on the examples on the official website, so please feel free to use it! After adding data, we will create a test set and write some data. Create two objects j and t and save them to the set. In
In addition to the tangle of queries, the rest are relatively simple! In fact, when I learned how to add, delete, modify, and query mongodb command lines, I also tried it one by one based on the examples on the official website, so please feel free to use it! After adding data, we will create a test set and write some data. Create two objects j and t and save them to the set. In
In addition to the tangle of queries, the rest are relatively simple!
In fact, when I learned how to add, delete, modify, and query mongodb command lines, I also tried it one by one based on the examples on the official website, so please feel free to use it!
Add data
Next we will create a test set and write some data. Create two objects j and t and save them to the set. In the example, ">" indicates 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}
>
Pay attention to the following points:
1. You do not need to create a set in advance. It is automatically created when data is inserted for the first time.
2. Data of any structure can be stored in the Document. Of course, in actual application, we still store a set of documents of the same type. this feature can be very flexible in applications. You do not need to modify your data structure like the alter table statement.
3. Each time data is inserted, an ID is included in the set named _ id, which will be detailed later!
Next, add some data (cyclically add ):
> For (var I = 1; I <10; 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 ")}
Note that the number of loops here is 10, but only 8th are displayed. There are still 2 data records not displayed. If you want to continue querying the following data, you only need to use the "it" command to continue displaying the following data:
{"_ Id": ObjectId ("4c220a42f3924d31102bd866"), "x": 4, "j": 17}
{"_ Id": ObjectId ("4c220a42f3924d31102bd867"), "x": 4, "j": 18}
Has more
> It
{"_ Id": ObjectId ("4c220a42f3924d31102bd868"), "x": 4, "j": 19}
{"_ Id": ObjectId ("4c220a42f3924d31102bd869"), "x": 4, "j": 20}
Technically speaking, find () returns a cursor object. However, in the above example, a cursor variable is not obtained. So shell automatically traverses the cursor, returns an initialized set, and allows us to continue to use it iteration output. Of course, we can also directly use the cursor for output, but this is the content of the "cursor" section.
_ Id key
In the data types supported by MongoDB, _ id is its own product. Let's give a brief introduction to it. Each document stored in the MongoDB set has a default primary key _ id. The primary key name is fixed and can be any data type supported by MongoDB. The default value is ObjectId. In the schema design of relational databases, most primary keys are numeric, such as common int and long values. More often, the values of primary keys are obtained by auto-increment databases, the ordering of such primary key values sometimes shows some logic. In contrast, MongoDB was originally designed for a distributed storage system, so it does not support auto-incrementing primary keys.
_ Id key example:
When we write a document into 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}
An extra key of the ObjectId type is provided here, which is not specified during insertion. This is similar to the rowid information of Oracle and is automatically generated. In MongoDB, each set must have a field named _ id. The default field type is ObjectId. In other words, the field type can be not 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 _ id type can be specified freely, it must be unique in the same set. If duplicate values are inserted, the system will throw an exception, as shown below:
> Db. c1.insert ({_ id: 3, name: "Bill_new", age: 55 })
E11000 duplicate key error index: test. c1. $ _ id _ dup key: {: 3.0}
>
Query data
Common Query
Before in-depth query, Let's first look at how to return a cursor object from a query. you can simply query by find (), which returns a set of arbitrary structures. if you implement a specific query, we will explain it later.
Implement the same query above, and then use while to output:
> 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 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 );
{"_ 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 MongoDB shell, we can also use the cursor as an array:
> Var cursor = db. things. find ();
> Printjson (cursor [4]);
{"_ Id": ObjectId ("4c220a42f3924d31102bd858"), "x": 4, "j": 3}
When using a cursor, pay attention to the memory usage, especially for large cursor objects, which may cause memory overflow. therefore, we should use Iteration Methods to output data. 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 MongoDB shell, rather than all other application drivers. The MongoDB cursor object does not have a snapshot. If another user calls next () for the first or last time in the set, you may not get the data in the cursor. therefore, you must explicitly lock the cursor you want to query.
Conditional Query
Now we know how to implement a query from the cursor and return data objects. Next we will look at how to query based on the specified conditions.
The following example shows how to execute a query similar to SQL and how to implement it in MongoDB. This is a query in MongoDB shell. Of course, you can also use other application drivers or languages:
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 conditions are {a: A, B: B ,... } Similar to "where a = A and B = B and ...".
The above shows all elements. Of course, we can also return specific elements, similar to the value of a field in the returned table, only need to find ({x: 4 }) the name of the specified element.
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 convenience, MongoDB shell provides a findOne () function to avoid the overhead caused by the cursor. this function is the same as the find () function, but it returns the first data in the cursor or returns null, that is, null data. As an example, name = "mongo" can be implemented in many ways. You can use next () to loop the cursor or return the first element as an array, but use findOne () the method is simpler and more efficient:
> Printjson (db. things. findOne ({name: "mongo "}));
{"_ Id": ObjectId ("4c2209f9f3924d31102bd84a"), "name": "mongo "}
To limit the length of the result set, you can call the limit method. This is a strongly recommended solution to performance problems, that is, to reduce network transmission by limiting the number of entries, 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 record
Change the name of the record whose name is mongo to mongo_new
> Db. things. update ({name: "mongo" },{$ set: {name: "mongo_new "}});
Let's check whether it has been changed.
> Db. things. find ();
{"_ Id": ObjectId ("4faa9e7dedd27e6d86d86371"), "x": 3}
{"_ Id": ObjectId ("4faa9e7bedd27e6d86d86370"), "name": "pai_new "}
Delete record
Delete records whose user name is pai_new from the things set,
> Db. things. remove ({name: "mongo_new "});
> Db. things. find ();
{"_ Id": ObjectId ("4faa9e7dedd27e6d86d86371"), "x": 3}
It is verified that the record is indeed deleted.
At this point, the case of addition, deletion, modification, and difference is almost complete. Or if there are any incomplete cases, you are welcome to leave a message!