Mongo # 查看数据库 > show dbs admin 0.000GB config 0.000GB local 0.000GB # 如果数据库不存在,则创建数据库,否则切换到指定数据库 > use local switched to db local # 查看当前使用的数据库 > db local # 跟传统关系型数据库术语对比 # 文档不需要设置相同的字段,并且相同的字段不需要相同的数据类型  Capped collections 固定大小的集合 # 链接数据库 mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]] # 创建数据库 use hello # 创建数据库表即集合 # 在 MongoDB 中,集合只有在内容插入后才会创建! 就是说,创建集合(数据表)后要再插入一# 个文档(记录),集合才会真正创建。 > use hello switched to db hello > db.t1.insert({name: 1111}) WriteResult({ "nInserted" : 1 }) > show tables t1 # 删除当前数据库的集合 > show tables t1 > db.t1.drop() # 删除当前数据库 > db.dropDatabase() { "dropped" : "hello", "ok" : 1 } # 创建集合的选项参数  > db haha # 这里设置了最大文档创建数量为1,所以最大文档一个文档 > db.createCollection('t2', {capped: true, size: 1024, max: 1 }) { "ok" : 1 } > show tables t2 > db.t2.insert({name: 1}) WriteResult({ "nInserted" : 1 }) > db.t2.insert({name: 2}) WriteResult({ "nInserted" : 1 }) # 这里查看创建的文档 只有一个 > db.t2.find().pretty() { "_id" : ObjectId("5efd91e6f59f926a3402a5c2"), "name" : 2 } # 一次创建多个文档 # 可以将要创建的文档赋予变量 > docs=([{name: 1}, {name: 2}]) [ { "name" : 1 }, { "name" : 2 } ] > db.t3.insertMany(docs) { "acknowledged" : true, "insertedIds" : [ ObjectId("5efd9337f59f926a3402a5c3"), ObjectId("5efd9337f59f926a3402a5c4") ] } > db.t3.find() { "_id" : ObjectId("5efd9337f59f926a3402a5c3"), "name" : 1 } { "_id" : ObjectId("5efd9337f59f926a3402a5c4"), "name" : 2 } # 更新文档数据 参数query 相当于where,参数update 相当于 db.collection.update(query, update, { upsert: false # 如果不存在update的记录 是否新增 multi: false, 是否把按条件查出来的多条记录全部更新 }) > db.t3.insertMany([{name: 3, sex: 'male'}, {name: 4, sex: 'female'}]) { "acknowledged" : true, "insertedIds" : [ ObjectId("5efd9544f59f926a3402a5c5"), ObjectId("5efd9544f59f926a3402a5c6") ] } > db.t3.find() { "_id" : ObjectId("5efd9337f59f926a3402a5c3"), "name" : 1 } { "_id" : ObjectId("5efd9337f59f926a3402a5c4"), "name" : 2 } { "_id" : ObjectId("5efd9544f59f926a3402a5c5"), "name" : 3, "sex" : "male" } { "_id" : ObjectId("5efd9544f59f926a3402a5c6"), "name" : 4, "sex" : "female" } > db.t3.update({name: {$gt: 2}}, {$set: {'name': 5}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.t3.find() # 只有符合条件的第一条更新了 { "_id" : ObjectId("5efd9337f59f926a3402a5c3"), "name" : 1 } { "_id" : ObjectId("5efd9337f59f926a3402a5c4"), "name" : 2 } { "_id" : ObjectId("5efd9544f59f926a3402a5c5"), "name" : 5, "sex" : "male" } { "_id" : ObjectId("5efd9544f59f926a3402a5c6"), "name" : 4, "sex" : "female" } # 设置multi为true时,符合条件的都更新了 > db.t3.update({name: {$gt: 2}}, {$set: {'name': 5}}, {multi: true}) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 1 }) > db.t3.find() { "_id" : ObjectId("5efd9337f59f926a3402a5c3"), "name" : 1 } { "_id" : ObjectId("5efd9337f59f926a3402a5c4"), "name" : 2 } { "_id" : ObjectId("5efd9544f59f926a3402a5c5"), "name" : 5, "sex" : "male" } { "_id" : ObjectId("5efd9544f59f926a3402a5c6"), "name" : 5, "sex" : "female" } # 设置upset为true时,不符合查询条件的直接新增该文档到该集合 > db.t3.update({name: {$gt: 10}}, {$set: {'name': 1000}}, {upsert: true}) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : ObjectId("5efd97be7e1ed95a7d6f0742") }) > db.t3.find() { "_id" : ObjectId("5efd9337f59f926a3402a5c3"), "name" : 1 } { "_id" : ObjectId("5efd9337f59f926a3402a5c4"), "name" : 2 } { "_id" : ObjectId("5efd9544f59f926a3402a5c5"), "name" : 5, "sex" : "male" } { "_id" : ObjectId("5efd9544f59f926a3402a5c6"), "name" : 5, "sex" : "female" } { "_id" : ObjectId("5efd97be7e1ed95a7d6f0742"), "name" : 1000 } # 删除文档 db.collection.remove(query, { justOne: false, # 是否只删除符合条件的一条 }) # 删除符合条件的全部 > db.t3.remove({name: {$gt: 3}}) WriteResult({ "nRemoved" : 3 }) > db.t3.find() { "_id" : ObjectId("5efd9337f59f926a3402a5c3"), "name" : 1 } { "_id" : ObjectId("5efd9337f59f926a3402a5c4"), "name" : 2 } # 只删除了一条 > db.t3.remove({name: {$gt: 0}}, {justOne: true}) WriteResult({ "nRemoved" : 1 }) > db.t3.find() { "_id" : ObjectId("5efd9337f59f926a3402a5c4"), "name" : 2 } # 删除全部文档 > db.t3.remove({}) WriteResult({ "nRemoved" : 1 }) > db.t3.find() # 新的删除方法 deleteMany deleteOne 分别是删除符合条件的多个和一个 > db.t3.insertMany([{name: 3, sex: 'male'}, {name: 4, sex: 'female'}]) { "acknowledged" : true, "insertedIds" : [ ObjectId("5efd9af7f59f926a3402a5c7"), ObjectId("5efd9af7f59f926a3402a5c8") ] } > db.t3.find() { "_id" : ObjectId("5efd9af7f59f926a3402a5c7"), "name" : 3, "sex" : "male" } { "_id" : ObjectId("5efd9af7f59f926a3402a5c8"), "name" : 4, "sex" : "female" } # 删除一个 > db.t3.deleteOne({name: 3}) { "acknowledged" : true, "deletedCount" : 1 } > db.t3.find() { "_id" : ObjectId("5efd9af7f59f926a3402a5c8"), "name" : 4, "sex" : "female" } # 删除全部 > db.t3.deleteMany({}) { "acknowledged" : true, "deletedCount" : 1 } > db.t3.find() # 查询文档 db.collection.find() db.collection.find().pretty()  # AND > db.t3.find() { "_id" : ObjectId("5efd9e06f59f926a3402a5c9"), "name" : 3, "sex" : "male" } { "_id" : ObjectId("5efd9e06f59f926a3402a5ca"), "name" : 4, "sex" : "female" } > db.t3.find({name: 3, sex: 'male'}) { "_id" : ObjectId("5efd9e06f59f926a3402a5c9"), "name" : 3, "sex" : "male" } # OR > db.t3.find({$or: [{name: 3}, {name: 4}]}) { "_id" : ObjectId("5efd9e06f59f926a3402a5c9"), "name" : 3, "sex" : "male" } { "_id" : ObjectId("5efd9e06f59f926a3402a5ca"), "name" : 4, "sex" : "female" } # limit 限制返回数量 db.t3.find().limit(2) # SKIP 跳过指定数量后的数据 > db.t3.find().limit(1) { "_id" : ObjectId("5efda0476dbcc89de406890f"), "name" : 3, "sex" : "male" } > db.t3.find().limit(1).skip(1) { "_id" : ObjectId("5efda0476dbcc89de4068910"), "name" : 4, "sex" : "female" } # sort 排序 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序排列 > db.t3.find().sort({sort: 1}) { "_id" : ObjectId("5efda0476dbcc89de406890f"), "name" : 3, "sex" : "male" } { "_id" : ObjectId("5efda0476dbcc89de4068910"), "name" : 4, "sex" : "female" } > db.t3.find().sort({sort: -1}) { "_id" : ObjectId("5efda0476dbcc89de406890f"), "name" : 3, "sex" : "male" } { "_id" : ObjectId("5efda0476dbcc89de4068910"), "name" : 4, "sex" : "female" } # 创建索引 # keys 值为要创建的索引字段,1 为指定按升序创建索引,按降序来创建索引为 -1 db.collection.createIndex(keys, options) > db.t4.insertMany([{name: 3, sex: 'male'}, {name: 4, sex: 'female'}]) { "acknowledged" : true, "insertedIds" : [ ObjectId("5efda7876dbcc89de4068911"), ObjectId("5efda7876dbcc89de4068912") ] } > db.t4.createIndex({sex: 1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } # 查看集合索引 > db.t4.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "leyi.t4" }, { "v" : 2, "key" : { "sex" : 1 }, "name" : "sex_1", "ns" : "leyi.t4" } ] > # aggregate 聚合 $group相当于 group by > db.hello.find() { "_id" : ObjectId("5efdacb483d0bf0f99239075"), "title" : "MongoDB Overview", "description" : "MongoDB is no sql database", "by_user" : "runoob.com", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5efdad8a83d0bf188d0cf953"), "title" : "NoSQL Overview", "description" : "No sql database is very fast", "by_user" : "runoob.com", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 10 } { "_id" : ObjectId("5efdad9983d0bf188d0cf954"), "title" : "Neo4j Overview", "description" : "Neo4j is no sql database", "by_user" : "Neo4j", "url" : "http://www.neo4j.com", "tags" : [ "neo4j", "database", "NoSQL" ], "likes" : 750 } # _id :强制必须存在,可以为 null。 > db.hello.aggregate([{$group: {_id: '$by_user', total: {$sum: 1}}}]) { "_id" : "runoob.com", "total" : 2 } { "_id" : "Neo4j", "total" : 1 } # 聚合的一些表达式  # $push > db.hello.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}]) { "_id" : "runoob.com", "url" : [ "http://www.runoob.com", "http://www.runoob.com" ] } { "_id" : "Neo4j", "url" : [ "http://www.neo4j.com" ] } # $push $addToSet 区别 $push只是将值加入一个数组中,并不管相同的值是否已经在数组中存在了,也就是说这是不去重的。 $addToSet是将值加入一个数组中,若相同的值在数组中已经存在了,则不再重复加入,也就是说这是去重的。 # 聚合管道 # 参数可以是数组格式,多个管道操作一通后输出结果  # $project 输出控制数据列的显示 1 显示列 0 隐藏该列 > db.hello.aggregate({$project: {_id: 0,title: 1}}) { "title" : "MongoDB Overview" } { "title" : "NoSQL Overview" } { "title" : "Neo4j Overview" } # 使用两个管道操作 $match likes> 10的 $project输出title列结果 > db.hello.aggregate([{$match: {likes: {$gt: 10}}}, {$project: {_id: 0,title: 1}}]) { "title" : "MongoDB Overview" } { "title" : "Neo4j Overview" }