本文专门介绍MongoDB的命令行操作。其实,这些操作在MongoDB官网提供的Quick Reference上都有,但是英文的,为了方便,这里将其稍微整理下,方便查阅。
这里用来做测试的是远端(10.77.20.xx)的Mongo数据库。
1、登录和退出
mongo命令直接加MongoDB服务器的IP地址(比如:mongo 10.77.20.xx),就可以利用Mongo的默认端口号(27017)登陆Mongo,然后便能够进行简单的命令行操作。
至于退出,直接exit,然后回车就好了。
$ mongo 10.77.20.xx MongoDB shell version: 2.0.4 connecting to: 10.77.20.xx/test PRIMARY> show collections PRIMARY> exit bye
从以上可以看出,登录后mongo会自动连上一个名为test的数据库。如果这个数据库不存在,那么mongo会自动建立一个名为test的数据库。上面的例子,由于Mongo服务器上没有名为test的db,因此,mongo新建了一个空的名为test的db。其中,没有任何collection。
2、database级操作
2.1 查看服务器上的数据库 > show dbs admin (empty) back_up (empty) blogtest 0.203125GB local 44.056640625GB test (empty) 2.2 切换数据库 切换到blogtest数据库(从默认的test数据库) > use blogtest switched to db blogtest mongo中,db代表当前使用的数据库。这样,db就从原来的test,变为现在的blogtest数据库。 2.3 查看当前数据库中的所有集合 > show collections book system.indexes user 2.4 创建数据库 mongo中创建数据库采用的也是use命令,如果use后面跟的数据库名不存在,那么mongo将会新建该数据库。不过,实际上只执行use命令后,mongo是不会新建该数据库的,直到你像该数据库中插入了数据。 > use test2 switched to db test2 PRIMARY> show dbs admin (empty) back_up (empty) blogtest 0.203125GB local 44.056640625GB test (empty) 到这里并没有看到刚才新建的test2数据库。 PRIMARY> db.hello.insert({"name":"testdb"}) 该操作会在test2数据库中新建一个hello集合,并在其中插入一条记录。 PRIMARY> show dbs admin (empty) back_up (empty) blogtest 0.203125GB local 44.056640625GB test (empty) test2 0.203125GB > show collections hello system.indexes 这样,便可以看到mongo的确创建了test2数据库,其中有一个hello集合。 2.5 删除数据库 > db.dropDatabase() { "dropped" : "test2", "ok" : 1 } > show dbs admin (empty) back_up (empty) blogtest 0.203125GB local 44.056640625GB test (empty) 2.6 查看当前数据库 > db test2 可以看出删除test2数据库之后,当前的db还是指向它,只有当切换数据库之后,test2才会彻底消失。
3、collection级操作
3.1 新建collection > db.createCollection("Hello") { "ok" : 1 } PRIMARY> show collections Hello system.indexes 从上面2.4也可以看出,直接向一个不存在的collection中插入数据也能创建一个collection。 > db.hello2.insert({"name":"lfqy"}) PRIMARY> show collections Hello hello2 system.indexes 3.2 删除collection > db.Hello.drop() true 返回true说明删除成功,false说明没有删除成功。 > db.hello.drop() false 不存在名为hello的collection,因此,删除失败。 3.3 重命名collection 将hello2集合重命名为HELLO > show collections hello2 system.indexes PRIMARY> db.hello2.renameCollection("HELLO") { "ok" : 1 } PRIMARY> show collections HELLO system.indexes 3.4 查看当前数据库中的所有collection >show collections
4、针对集合中记录的操作
这一小节从这里开始,我们用事先存在的blogtest数据库做测试,其中有两个Collection,一个是book,另一个是user。
4.1 插入操作
4.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}) 4.1.2 同样也可以用save完成类似的插入操作 > db.user.save({'name':'Wentworth Earl Miller','gender':'male','age':41,'salary':33000})
4.2 查找操作
4.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 }
4.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.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 }
4.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表示。
4.2.5 查询指定字段的数据,并去重。
查询gender字段的数据,并去掉重复数据 > db.user.distinct('gender') [ "male", "female" ]
4.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 }
4.2.7 统计查询结果中记录的条数
(1)统计集合中的所有记录条数 > db.user.find().count() 5 (2)查询符合条件的记录数 查询salary小于4000或大于10000的记录数 > db.user.find({$or: [{salary: {$lt:4000}}, {salary: {$gt:10000}}]}).count() 4
4.3 删除操作
4.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 PRIMARY> db.test.find() 可见test中的记录全部被删除。 注意db.collection.remove()和drop()的区别,remove()只是删除了集合中所有的记录,而集合中原有的索引等信息还在,而drop()则把集合相关信息整个删除(包括索引)。
4.3.2 删除集合中符合条件的所有记录
> db.user.remove({name:'lfqy'}) PRIMARY> 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 } PRIMARY> db.user.remove( {salary :{$lt:10}}) PRIMARY> 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 }
4.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 更新操作
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.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的东西。
总之,希望对自己,对大家都有所帮助。