时间:2021-07-01 10:21:17 帮助过:4人阅读
可以看出删除test2数据库之后,当前的db还是指向它,只有当切换数据库之后,test2才会彻底消失。
> 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 }
> 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()可以查找符合条件的第一条记录。 [plain] 将上面的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 查询记录的指定字段 [plain] 查询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 查询指定字段的数据,并去重。 [plain] 查询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 统计查询结果中记录的条数 [plain] (1)统计集合中的所有记录条数 > db.user.find().count() 5 (2)查询符合条件的记录数 查询salary小于4000或大于10000的记录数 > db.user.find({$or: [{salary: {$lt:4000}}, {salary: {$gt:10000}}]}).count() 4.3 删除操作 4.3.1 删除整个集合中的所有数据 [plain] > 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()则把集合相关信息整个删除(包括索引)。 4.3.2 删除集合中符合条件的所有记录
[plain] > 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 }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 }
MongoDB 基础命令行
标签: