Mongodb daily operations (query) first introduces daily addition, deletion, modification, query: 1: Query showcollections ---- view the collection db under the database. collection-name.find () -------- the most basic query db directly followed by the name of the Set, can not fully display the data of the real set, you can use it to display db. collection-name.findOn
Mongodb routine operations (query) first introduces the daily addition, deletion, modification, query: 1: query show collections ---- view the collection db under the database. collection-name.find () -------- the most basic query db directly followed by the name of the Set, can not fully display the data of the real set, you can use it to display db. collection-name.findOn
Mongodb routine operations (query)
First, we will introduce daily addition, deletion, modification, and query:
1: Query
Show collections ---- view the collections in the database
Db. collection-name.find () -------- the most basic query db is directly followed by the name of the Set, can not fully display the data of the real set, you can use it display
Db. collection-name.findOne () ---------- be sure to pay attention to uppercase, return the first record of the Set, if there is no data, is NULL
Db. collection-name.find (). forEach (printjson); displays all rows
Conditional query:
Example:> db. post. find ();
{"_ Id": ObjectId ("4eae0669b81d17242113d9c4"), "name": "docments "}
{"_ Id": ObjectId ("4eae067ab81d17242113d9c5"), "age": 60}
Db. post. find ({age: 60}). forEach (printjson); = select * from post where age = 60 ------ show all doc FILES whose age is 60
Db. post. find ({age: 60}, {name: true }). forEach (printjson) = select name from post where age = 60 --- display all name columns whose age is 60
Db. post. find (). limit (3) = select * from where rownum <= 3;
Db. post. update ({name: "docments" },{$ set: {name: "newdoc "}}) ---- the record for updating name = "docments" is name = "newdoc". Only the first row is updated.
Db. post. remove ({name: "newdoc"}) = delete from post where name = "newdoc ";
Conditional operation query:
Db. collection. find ({"field": {$ gt: value}); // greater than: field> value
Db. collection. find ({"field": {$ lt: value}); // less than: field <value
Db. collection. find ({"field": {$ gte: value}); // greater than or equal to: field> = value
Db. collection. find ({"field": {$ lte: value}); // less than or equal to: field <= value
Db. collection. find ({"field": {$ ne: value}); // not equal to: field! = Value
Db. users. find ({age: {$ all: [20, 30]});
{Name: 'David ', age: 26, age: [20, 30, 40]}
However, no {name: 'David ', age: 26, age: [20, 7, 9]} can be found.
Db. users. find ({age: {$ exists: true}); --- query records with sge Fields
Db. post. find ({age: {$ exists: false}); -- query records that do not contain the age Field
Db. post. insert ({name: "docments", age: null })
Db. post. find ({age: null}) queries records whose age is null
{"_ Id": ObjectId ("4eae0a2eb81d17242113d9cb"), "name": "docments", "newname": "newdoc "}
{"_ Id": ObjectId ("4eb89d1023c8705b173a80f0"), "name": "docments", "age": null}
The records without age are displayed.
Db. post. find ({age: {"$ in": [null], "$ exists": true}) to check records whose age is null.
Db. post. find ({age :{$ mod: [7, 4]}) modulo 7, select * from post where mod (age, 7) = 4 for the remaining 4 records
Db. post. find ({age :{$ not :{$ mod: [7, 4] }}) = select * from post where mod (age, 7 )! = 4
{"_ Id": ObjectId ("4eae067ab81d17242113d9c5"), "age": 60, "name": "hank "}
Db. post. find ({name:/doc/I}) # case insensitive
Query the data of x in the range of 2, 4, and 6.
Db. things. find ({x: {$ in: [2, 4, 6]}); = select * from things where x in (2, 4, 6 );
Db. post. find ({age: {$ nin: [60, 70]}) = select * from post where age not in (60, 70) -- here is actually the same as null, if the key "age" is not displayed
$ Size matches the number in the array
{"_ Id": ObjectId ("4eb8c06823c8705b173a80fa"), "num": [1, 2, 3]}
Db. post. find ({num: {$ size: 3}) ---- You can view the above record.
Db. post. find (). count () --- count records
Db. post. find ({name:/^ d/I}) -- match the record with the first letter of name d, followed by I, and show the case
Db. post. find (). skip (3). limit (2) -- after skipping three records, the first two
Db. post. find () sort ({age:-1}) -- sort by age in descending order. The Ascending Order is 1.