insert() 方法
要插入数据到 MongoDB 集合,需要使用 MongoDB 的 insert() 或 save() 方法。
插入文档:db.COLLECTION_NAME.insert(document)
> show collections
col_1
col_2
runoob
system.indexes
> db.mycol.insert({"title":"MongoDb overview"})#如果之前该集合不存在,则重新创建集合
WriteResult({ "nInserted" : 1 })#成功插入文档
> show collections
col_1
col_2
mycol
runoob
system.indexes
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "title" : "MongoDb overview" }
MongoDB的 update() 和 save() 方法用于更新文档的集合。 update()方法更新现有的文档值,而替换现有的文档通过的文件中 save() 方法。
MongoDB Update() 方法
update()方法更新现有文档值。
更新文档:db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "title" : "MongoDb overview" }
{ "_id" : ObjectId("57906dd3b2dade9538e2ac1f"), "title" : "alili MongoDb" }
{ "_id" : ObjectId("57906ddfb2dade9538e2ac20"), "title" : "mongodb" }
> db.mycol.update({"name":"no name"})#做了个测试
2016-07-21T14:39:29.359+0800 E QUERY Error: need an object
at Error (<anonymous>)
at DBCollection._parseUpdate (src/mongo/shell/collection.js:400:21)
at DBCollection.update (src/mongo/shell/collection.js:432:23)
at (shell):1:10 at src/mongo/shell/collection.js:400
> db.mycol.update({"title":"mongodb"},{$set:{"title":"change mongodb"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "title" : "MongoDb overview" }
{ "_id" : ObjectId("57906dd3b2dade9538e2ac1f"), "title" : "alili MongoDb" }
{ "_id" : ObjectId("57906ddfb2dade9538e2ac20"), "title" : "change mongodb" }
> db.mycol.update({"title" : "change mongodb"},{$set:{"name":"no name"}})#然后又做了一次改变,是在后面追加的
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "title" : "MongoDb overview" }
{ "_id" : ObjectId("57906dd3b2dade9538e2ac1f"), "title" : "alili MongoDb" }
{ "_id" : ObjectId("57906ddfb2dade9538e2ac20"), "title" : "change mongodb", "name" : "no name" }
MongoDB Save() 方法
save() 方法替换现有的文档和通过新的文档 save() 方法
语法
MongoDB 的 save() 方法的基本语法如下:
替换现有文档:db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "title" : "MongoDb overview" }
{ "_id" : ObjectId("57906dd3b2dade9538e2ac1f"), "title" : "alili MongoDb" }
{ "_id" : ObjectId("57906ddfb2dade9538e2ac20"), "title" : "change mongodb", "name" : "no name" }
> db.mycol.save({"_id" : ObjectId("57906a93b2dade9538e2ac1e"),"name":"mongodb","by":"save"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "name" : "mongodb", "by" : "save" }
{ "_id" : ObjectId("57906dd3b2dade9538e2ac1f"), "title" : "alili MongoDb" }
{ "_id" : ObjectId("57906ddfb2dade9538e2ac20"), "title" : "change mongodb", "name" : "no name" }
>
MongoDB的 remove() 方法用于从集合中删除文档。remove() 方法接受两个参数。第一个是删除criteria ,第二是justOne标志:
deletion criteria :(可选)删除标准,根据文件将被删除。
justOne : (可选)如果设置为true或1,然后只删除一个文件。
删除文档:db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "name" : "mongodb", "by" : "save" }
{ "_id" : ObjectId("57906dd3b2dade9538e2ac1f"), "title" : "alili MongoDb" }
{ "_id" : ObjectId("57906ddfb2dade9538e2ac20"), "title" : "change mongodb", "name" : "no name" }
> db.mycol.remove({"title":"alili MongoDb"})
WriteResult({ "nRemoved" : 1 })
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "name" : "mongodb", "by" : "save" }
{ "_id" : ObjectId("57906ddfb2dade9538e2ac20"), "title" : "change mongodb", "name" : "no name" }
> db.mycol.insert({"name":""})
WriteResult({ "nInserted" : 1 })
> db.mycol.insert({"name":""})
WriteResult({ "nInserted" : 1 })
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "name" : "mongodb", "by" : "save" }
{ "_id" : ObjectId("57906ddfb2dade9538e2ac20"), "title" : "change mongodb", "name" : "no name" }
{ "_id" : ObjectId("579077d9b2dade9538e2ac21"), "name" : "" }
{ "_id" : ObjectId("579077dbb2dade9538e2ac22"), "name" : "" }
> db.mycol.remove({"name":""})#相同的都会删去的
WriteResult({ "nRemoved" : 2 })
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "name" : "mongodb", "by" : "save" }
{ "_id" : ObjectId("57906ddfb2dade9538e2ac20"), "title" : "change mongodb", "name" : "no name" }
>
只删除一个:db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "name" : "mongodb", "by" : "save" }
{ "_id" : ObjectId("57906ddfb2dade9538e2ac20"), "title" : "change mongodb", "name" : "no name" }
{ "_id" : ObjectId("5790789fb2dade9538e2ac23"), "age" : "" }
{ "_id" : ObjectId("579078a1b2dade9538e2ac24"), "age" : "" }
{ "_id" : ObjectId("579078a4b2dade9538e2ac25"), "age" : "" }
> db.mycol.remove({"age":""},1)
WriteResult({ "nRemoved" : 1 })
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "name" : "mongodb", "by" : "save" }
{ "_id" : ObjectId("57906ddfb2dade9538e2ac20"), "title" : "change mongodb", "name" : "no name" }
{ "_id" : ObjectId("579078a1b2dade9538e2ac24"), "age" : "" }
{ "_id" : ObjectId("579078a4b2dade9538e2ac25"), "age" : "" }
>
删除集合的所有的内容:db.COLLECTION_NAME.remove({})
> db.mycol.find()
{ "_id" : ObjectId("57906a93b2dade9538e2ac1e"), "name" : "mongodb", "by" : "save" }
{ "_id" : ObjectId("57906ddfb2dade9538e2ac20"), "title" : "change mongodb", "name" : "no name" }
{ "_id" : ObjectId("579078a1b2dade9538e2ac24"), "age" : "" }
{ "_id" : ObjectId("579078a4b2dade9538e2ac25"), "age" : "" }
> db.mycol.remove()#抛出了异常,去搜了下
2016-07-21T15:33:45.122+0800 E QUERY Error: remove needs a query
at Error (<anonymous>)
at DBCollection._parseRemove (src/mongo/shell/collection.js:305:32)
at DBCollection.remove (src/mongo/shell/collection.js:328:23)
at (shell):1:10 at src/mongo/shell/collection.js:305
> db.mycol.remove({})
WriteResult({ "nRemoved" : 4 })
> db.mycol.find()
>