1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| 语法: db.COLLECTION_NAME.insert(document) # MongoDB 使用 insert() 或 save() 方法向集合中插入文档 3.2 版本后还有以下几种语法可用于插入文档: db.collection.insertOne() # 向指定集合中插入一条文档数据 db.collection.insertMany() # 向指定集合中插入多条文档数据 # 文档的数据结构和JSON基本一样。所有存储在集合中的数据都是BSON格式。BSON是一种类json的一种二进制形式的存储格式,简称Binary JSON。
hqmongodb:PRIMARY> db.col.insert({title:'MongoDB教程', description:'MongoDB是一个Nosql数据库', by:'教程', url:'http://www.runoob.com', tags:['mongodb','database','NoSQL'], likes:100}) WriteResult({ "nInserted" : 1 }) # 使用insert()方法插入文档,每对键值间用逗号分隔 hqmongodb:PRIMARY> db.col.find() { "_id" : ObjectId("5c822dfa1fb12ed017cf9362"), "title" : "MongoDB教程", "description" : "MongoDB是一个Nosql数据库", "by" : "教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } # 使用find()方法查看 hqmongodb:PRIMARY> document=({title:'MongoDB教程', description:'MongoDB是一个Nosql数据库', by:'教程', url:'http://www.runoob.com', tags:['mongodb','database','NoSQL'], likes:100}) { "title" : "MongoDB教程", "description" : "MongoDB是一个Nosql数据库", "by" : "教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } # 将数据定义在一个变量中 hqmongodb:PRIMARY> db.col.insert(document) WriteResult({ "nInserted" : 1 }) # 再插入变量。效果与插入数据是一样的 hqmongodb:PRIMARY> db.col.find() { "_id" : ObjectId("5c822dfa1fb12ed017cf9362"), "title" : "MongoDB教程", "description" : "MongoDB是一个Nosql数据库", "by" : "教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5c822e401fb12ed017cf9363"), "title" : "MongoDB教程", "description" : "MongoDB是一个Nosql数据库", "by" : "教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } # 查看插入了两个数据
hqmongodb:PRIMARY> var document = db.collection.insertOne({"a": 3}) hqmongodb:PRIMARY> document { "acknowledged" : true, "insertedId" : ObjectId("5c8233471fb12ed017cf9364") } # 插入单条数据并查看 hqmongodb:PRIMARY> var res = db.collection.insertMany([{"b":3},{'c':4}]) hqmongodb:PRIMARY> res { "acknowledged" : true, "insertedIds" : [ ObjectId("5c8233781fb12ed017cf9365"), ObjectId("5c8233781fb12ed017cf9366") ] } # 插入多条数据并查看
------------- 从节点 ------------- hqmongodb:SECONDARY> use runoob switched to db runoob hqmongodb:SECONDARY> db.col.find() Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 } hqmongodb:SECONDARY> rs.slaveOk() # 在从节点查看数据要使用此命令,不然会有上面的错误提示 hqmongodb:SECONDARY> db.col.find() { "_id" : ObjectId("5c822dfa1fb12ed017cf9362"), "title" : "MongoDB教程", "description" : "MongoDB是一个Nosql数据库", "by" : "教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5c822e401fb12ed017cf9363"), "title" : "MongoDB教程", "description" : "MongoDB是一个Nosql数据库", "by" : "教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
|