MongoDB additions and deletions to search:
Note: the "_id" field is the GUID that the database gives us by default, to ensure the uniqueness of the data
1. Add insert
1. Syntax: DB. Insert ({"Key1": value1, "Key2": value2,..... "KeyN": Valuen})
> Db.wqq.insert ({"Name": "Wqq", AGE:25,HEIGHT:1.65,DEP: "Degree"})
Writeresult ({"ninserted": 1})
# #插入可以指定_id的值, and other, nested between values
for (i=1;i<100;i++) Db.wqq.insert ({"_id": I,age:22+i,name: "AAA" +i})
Db. Save ({"Key1": value1, "Key2": value2,..... "KeyN": Valuen})//save () is not inserted if it already exists, and is inserted if it does not exist (refers to the value of _id)
Db.wqq.save ({"_id": +, "age": $, "name": "Aaa19"})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 0})
> Db.wqq.save ({"_id": 119, "Age": $, "name": "Aaa19"})
Writeresult ({"nmatched": 0, "nupserted": 1, "nmodified": 0, "_id": 119})
2. Insert multiple strips:
Db.abc.insert ([{name: "Wangwu", Age:22,sex:1,class: "JSJ"},{name: "AAA", Age:22,sex:1,class: "BDD"}])
2. Finding find
1. Find all: DB. Collection. Find ()
2. Condition Lookup: DB. Set. Find ({"Key1": value1, "Key2": value2,..... "KeyN": Valuen})
3. Other statistics
Db.wqq.count () <===>db.wqq.find (). Count ()//statistics
Db.wqq.find (). Sort ({name:-1})//Sort-1 to descending 1 for ascending
Db.wqq.find (). Limit (4)//number of
Db.wqq.find (). Limit (4). Pretty ()//format display
Db.wqq.find (). Skip (4). Limit (Ten)//skip a few values
Db.wqq.find (). Skip (4). Limit ({"_id":-1})//to fetch several values by sorting values
Db.wqq.find ({"Age": {$all: [110]}})
Db.wqq.find ({"Age": {$in: [110,111,112 ...]}}) Returns the value that exists in the array
Db.wqq.find ({"Age": {$nin: [110,111,112 ...]}}) Returns the value that exists in the array
Db.wqq.find ({"Age": {$exists: 1}}//Check if a field exists
Db.wqq.find ({"Age": {$mod: [10,2]}})//Returns the value of the field equal to the remainder after modulo
Db.wqq.find ({$or: [{"Age": 40},{"name": "Aaa19"}]})//View records that specify multiple criteria, similar to SQL or
Db.wqq.find ({$nor: [{"Age": 40},{"name": "Aaa19"}]})//contrary to or
# #比较运算符
$GT: Greater than $gte: greater than or equal to $LT: less than $lte: less than or equal to $ne: not equal to
Db.wqq.find ({"Age": {$lt: 100}})
# #逻辑运算
and operations:
Db.wqq.find ({age:23,name: "Aaa1"})
or ($in)/non-$nin
Db.wqq.find ({age:{$in: [11,222,33,444]}})
# #正则匹配
Db.wqq.find ({name:/aaa/})
# # $where
Returns the document containing the Login_name field
Db.getcollection (' users '). Find ({$where: function () {
Return!! This.login_name;
}})
# # and operation
Db.wqq.find ({name: "Wangwu", Class: "JSJ"})
# #组合查询
Db.abc.find ({name:{$exists: 1, $nin: ["Zhang San"]}})//field exists and is not called "Zhang San"
3. Update updates ()
1. Update a field:
Db.wqq.update ({age:42},{$set: {name: "SBT"}})
2, add value to a document or do not exist to increase the value of the field
Db.wqq.update ({name: "aaa78"},{$inc: {age:2}},0,1)
3, batch update, if the field is updated, if the field does not exist add field
Db.wqq.update ({name: "AAA"},{$set: {age:2}},false,true)
Db.wqq.update ({name: "Zhang San"},{$set: {age:3}},0,1)
4, a collection of only one key-value update
Db.wqq.update ({name: "Zhang San"},{name: "Ode to Lun"})
4. Delete
1. Delete some documents
Db.wqq.remove ({"Name": "ABC"})
2. Delete the entire collection:
Db.wqq.drop ()
3. Delete the first few
Db.wqq.remove ({name: "abc"},2)
4. Delete fields
Db.abc.update ({},{$unset: {age:50}},0,1)//delete multiple fields without adding 0, 1 delete the first field
5, the use of cursors
var x=db.wqq.find ();
X.next ()
X.hasnext ()
while (X.hasnext () ==true) {X.next ()}
6. Increase the array in the collection, Popup
Db.abc.update ({},{$push: {arr:3}})//Add a worthwhile array
Db.abc.update ({},{$pushAll: {arr:[1,2,3,4,5,6,7,8,9]}})//Add multiple worthwhile arrays
Db.abc.update ({},{$pop: {arr:1}})//Popup The last value of the array,-1 The first value of the arrays
Db.abc.update ({name: "Zhang San"},{$pull: {arr:4}},0,1)//delete the specified value of the array
Db.abc.update ({},{$addToSet: {arr:6}},0,1)//Adds an array to the array, and does not repeat if present
Db.abc.update ({name: "Zhang San"},{$addToSet: {email2:{$each: [1,2,3,4]}}},0,1]//used to run the modifier to manipulate data multiple times to the collection
This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1976406
Additions and deletions in MongoDB operation