1 插入操作
- 1.1 向user集合中插入两条记录
- > db.user.insert({'name':'Gal Gadot','gender':'female','age':28,'salary':11000})
- > db.user.insert({'name':'Mikie Hara','gender':'female','age':26,'salary':7000})
- 1.2 同样也可以用save完成类似的插入操作
- > db.user.save({'name':'Wentworth Earl Miller','gender':'male','age':41,'salary':33000})
2 查找操作
2.1 查找集合中的所有记录
- > db.user.find()
- { "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13 }
2.2 查找集合中的符合条件的记录
- (1)单一条件
- a)Exact Equal:
- 查询age为了23的数据
- > db.user.find({"age":23})
- { "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
- b)Great Than:
- 查询salary大于5000的数据
- > db.user.find({salary:{$gt:5000}})
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- c)Fuzzy Match
- 查询name中包含'a'的数据
- > db.user.find({name:/a/})
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- 查询name以G打头的数据
- > db.user.find({name:/^G/})
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- (2)多条件"与"
- 查询age小于30,salary大于6000的数据
- > db.user.find({age:{$lt:30},salary:{$gt:6000}})
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- (3)多条件"或"
- 查询age小于25,或者salary大于10000的记录
- > db.user.find({$or:[{salary:{$gt:10000}},{age:{$lt:25}}]})
- { "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- (4)不等于查询
- 查询年龄不等于23的记录(这里返回结果中,会包含没有年龄字段的记录)
- > db.user.find({"age":{ $ne: 23}})
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11050 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7050 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("524562d681c83a5bf26fc286"), "gender" : "female1", "salary" : 50 }
- { "_id" : ObjectId("524563e881c83a5bf26fc287"), "gender" : "x" }
- { "_id" : ObjectId("5245648081c83a5bf26fc288"), "gender" : "x" }
- { "_id" : ObjectId("5245648e81c83a5bf26fc289"), "age" : "x" }
- { "_id" : ObjectId("524564c181c83a5bf26fc28a"), "age" : "x", "gender" : 4 }
2.3 查询第一条记录
将上面的find替换为findOne()可以查找符合条件的第一条记录。- 将上面的find替换为findOne()可以查找符合条件的第一条记录。
- > db.user.findOne({$or:[{salary:{$gt:10000}},{age:{$lt:25}}]})
- {
- "_id" : ObjectId("52442736d8947fb501000001"),
- "name" : "lfqy",
- "gender" : "male",
- "age" : 23,
- "salary" : 15
- }
2.4 查询记录的指定字段
- 查询user集合中所有记录的name,age,salary,sex_orientation字段
- > db.user.find({},{name:1,age:1,salary:1,sex_orientation:true})
- { "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "age" : 23, "salary" : 15 }
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- 注意:这里的1表示显示此列的意思,也可以用true表示。
2.5 查询指定字段的数据,并去重。
- 查询gender字段的数据,并去掉重复数据
- > db.user.distinct('gender')
- [ "male", "female" ]
2.6 对查询结果集的操作
- (1)Pretty Print
- 为了方便,mongo也提供了pretty print工具,db.collection.pretty()或者是db.collection.forEach(printjson)
- > db.user.find().pretty()
- {
- "_id" : ObjectId("52442736d8947fb501000001"),
- "name" : "lfqy",
- "gender" : "male",
- "age" : 23,
- "salary" : 15
- }
- {
- "_id" : ObjectId("52453cfb25e437dfea8fd4f4"),
- "name" : "Gal Gadot",
- "gender" : "female",
- "age" : 28,
- "salary" : 11000
- }
- {
- "_id" : ObjectId("52453d8525e437dfea8fd4f5"),
- "name" : "Mikie Hara",
- "gender" : "female",
- "age" : 26,
- "salary" : 7000
- }
- {
- "_id" : ObjectId("52453e2125e437dfea8fd4f6"),
- "name" : "Wentworth Earl Miller",
- "gender" : "male",
- "age" : 41,
- "salary" : 33000
- }
- {
- "_id" : ObjectId("52454155d8947fb70d000000"),
- "name" : "not known",
- "sex_orientation" : "male",
- "age" : 13
- }
- (2)指定结果集显示的条目
- a)显示结果集中的前3条记录
- > db.user.find().limit(3)
- { "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- b)查询第1条以后的所有数据
- > db.user.find().skip(1)
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- c)对结果集排序
- 升序
- > db.user.find().sort({salary:1})
- { "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- 降序
- > db.user.find().sort({salary:-1})
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
2.7 统计查询结果中记录的条数
- (1)统计集合中的所有记录条数
- > db.user.find().count()
- 5
- (2)查询符合条件的记录数
- 查询salary小于4000或大于10000的记录数
- > db.user.find({$or: [{salary: {$lt:4000}}, {salary: {$gt:10000}}]}).count()
- 4
2.8 查询存在(或不存在)指定字段的记录
查询不存在age字段,但是有gender字段,并且ex为barrymore的记录。
- > db.user.find({"age":{$exists:false},"gender":{$exists:true},"ex":"barrymore"})
- { "_id" : ObjectId("524562d681c83a5bf26fc286"), "ex" : "barrymore", "gender" : "female1", "salary" : 50 }
- { "_id" : ObjectId("524563e881c83a5bf26fc287"), "ex" : "barrymore", "gender" : "x" }
- { "_id" : ObjectId("5245648081c83a5bf26fc288"), "ex" : "barrymore", "gender" : "x" }
3 删除操作
3.1 删除整个集合中的所有数据
- > db.test.insert({name:"asdf"})
- > show collections
- book
- system.indexes
- test
- user
- 到这里新建了一个集合,名为test。
- 删除test中的所有记录。
- > db.test.remove()
- PRIMARY> show collections
- book
- system.indexes
- test
- user
- > db.test.find()
- 可见test中的记录全部被删除。
- 注意db.collection.remove()和drop()的区别,remove()只是删除了集合中所有的记录,而集合中原有的索引等信息还在,而drop()则把集合相关信息整个删除(包括索引)。
3.2 删除集合中符合条件的所有记录
- > db.user.remove({name:'lfqy'})
- > db.user.find()
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- > db.user.find()
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("52455cc825e437dfea8fd4f8"), "name" : "2", "gender" : "female", "age" : 28, "salary" : 2 }
- { "_id" : ObjectId("52455d8a25e437dfea8fd4fa"), "name" : "1", "gender" : "female", "age" : 28, "salary" : 1 }
- > db.user.remove( {salary :{$lt:10}})
- > db.user.find()
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
3.3 删除集合中符合条件的一条记录
- > db.user.find()
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("52455de325e437dfea8fd4fb"), "name" : "1", "gender" : "female", "age" : 28, "salary" : 1 }
- { "_id" : ObjectId("52455de925e437dfea8fd4fc"), "name" : "2", "gender" : "female", "age" : 28, "salary" : 2 }
- > db.user.remove({salary :{$lt:10}},1)
- > db.user.find()
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("52455de925e437dfea8fd4fc"), "name" : "2", "gender" : "female", "age" : 28, "salary" : 2 }
- 当然,也可以是db.user.remove({salary :{$lt:10}},true)
4 更新操作
4.1 赋值更新
db.collection.update(criteria, objNew, upsert, multi )criteria:update的查询条件,类似sql update查询内where后面的objNew:update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的。upsert : 如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
- > db.user.find()
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "name" : "lfqy", "gender" : "male", "age" : 28, "salary" : 1 }
- { "_id" : ObjectId("5245607525e437dfea8fd4fe"), "name" : "lfqy", "gender" : "male", "age" : 28, "salary" : 2 }
- > db.user.update({name:'lfqy'},{$set:{age:23}},false,true)
- > db.user.find()
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 1 }
- { "_id" : ObjectId("5245607525e437dfea8fd4fe"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 2 }
- db.user.find()
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 1 }
- { "_id" : ObjectId("5245607525e437dfea8fd4fe"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 2 }
- > db.user.update({name:'lfqy1'},{$set:{age:23}},true,true)
- > db.user.find()
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 1 }
- { "_id" : ObjectId("5245607525e437dfea8fd4fe"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 2 }
- { "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
- > db.user.update({name:'lfqy'},{$set:{interest:"NBA"}},false,true)
- > db.user.find()
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
- { "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 1 }
- { "_id" : ObjectId("5245607525e437dfea8fd4fe"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 2 }
4.2 增值更新
- > db.user.find()
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
- { "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 1 }
- { "_id" : ObjectId("5245607525e437dfea8fd4fe"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 2 }
- > db.user.update({gender:'female'},{$inc:{salary:50}},false,true)
- > db.user.find()
- { "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11050 }
- { "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7050 }
- { "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
- { "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
- { "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
- { "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 1 }
- { "_id" : ObjectId("5245607525e437dfea8fd4fe"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 2 }
关于更新操作(db.collection.update(criteria, objNew, upsert, multi )),要说明的是,如果upsert为true,那么在没有找到符合更新条件的情况下,mongo会在集合中插入一条记录其值满足更新条件的记录(其中的字段只有更新条件中涉及的字段,字段的值满足更新条件),然后将其更新(注意,如果更新条件是$lt这种不等式条件,那么upsert插入的记录只会包含更新操作涉及的字段,而不会有更新条件中的字段。这也很好理解,因为没法为这种字段定值,mongo索性就不取这些字段)。如果符合条件的记录中没有要更新的字段,那么mongo会为其创建该字段,并更新。
上面大致介绍了MongoDB命令行中所涉及的操作,只是为了记录和查阅。细心的也许会发现,这篇文章,越往后我的耐心越少。期待有时间能分享一些not very navie的东西。
总之,希望对自己,对大家都有所帮助。