标签:
> a1=db.test.findOne()
{ "_id" : ObjectId("5738785d132e1e47e535a177"), "x" : 3.14, "y" : 5.5 }
> a1
{ "_id" : ObjectId("5738785d132e1e47e535a177"), "x" : 3.14, "y" : 5.5 }
> delete a1.x
true
> a1
{ "_id" : ObjectId("5738785d132e1e47e535a177"), "y" : 5.5 }
> a1.content={name:‘job‘,age:11}
{ "name" : "job", "age" : 11 }
> a1
{
"_id" : ObjectId("5738785d132e1e47e535a177"),
"y" : 5.5,
"content" : {
"name" : "job",
"age" : 11
}
}
> db.test.update({"_id" : ObjectId("5738785d132e1e47e535a177")},a1)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com" }
>
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$set":{phone:666}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com", "phone" : 666 }
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$set":{phone:888}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com", "phone" : 888 }
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$unset":{phone:888}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com" }
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com" }
{ "_id" : ObjectId("57395fa85f74882a9bfa2d9f"), "id" : { "phone" : 888, "address" : "abc" } }
>
> db.test.update({"_id" : ObjectId("57395fa85f74882a9bfa2d9f")},{"$set":{"id.phone":666}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com" }
{ "_id" : ObjectId("57395fa85f74882a9bfa2d9f"), "id" : { "phone" : 666, "address" : "abc" } }
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com" }
{ "_id" : ObjectId("57395fa85f74882a9bfa2d9f"), "id" : { "phone" : 666, "address" : "abc" } }
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$inc":{age:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 31, "email" : "xxxx@qq.com" }
{ "_id" : ObjectId("57395fa85f74882a9bfa2d9f"), "id" : { "phone" : 666, "address" : "abc" } }
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com", "phone" : 888 }
{ "_id" : ObjectId("57395fa85f74882a9bfa2d9f"), "id" : { "phone" : 666, "address" : "abc" } }
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$push":{comments:
... {name:"jack",content:"good"}
... }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com", "phone" : 888, "comments" : [ { "name" : "jack", "content" : "good" } ] }
{ "_id" : ObjectId("57395fa85f74882a9bfa2d9f"), "id" : { "phone" : 666, "address" : "abc" } }
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$push":{comments: {name:"tom",content:"very good"} }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com", "phone" : 888, "comments" : [ { "name" : "jack", "content" : "good" }, { "name" : "tom", "content" : "very good" } ] }
{ "_id" : ObjectId("57395fa85f74882a9bfa2d9f"), "id" : { "phone" : 666, "address" : "abc" } }
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$push":{comments: {"$each":["a","b","c"]}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
> db.test.findOne()
{
"_id" : ObjectId("573957c55f74882a9bfa2d9e"),
"name" : "brent",
"age" : 30,
"email" : "xxxx@qq.com",
"phone" : 888,
"comments" : [
{
"name" : "jack",
"content" : "good"
},
{
"name" : "tom",
"content" : "very good"
},
"a",
"b",
"c"
]
}
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$push":{comments: {"$each":["a","b","c"],"$slice":-10}}})
> db.test.update({"comments":{"$ne":"jack"}},{"$push":{"comments":"jack"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.findOne()
{
"_id" : ObjectId("573957c55f74882a9bfa2d9e"),
"name" : "brent",
"age" : 30,
"email" : "xxxx@qq.com",
"phone" : 888,
"comments" : [
{
"name" : "jack",
"content" : "good"
},
{
"name" : "tom",
"content" : "very good"
},
"a",
"b",
"c",
"a",
"b",
"c",
"jack"
]
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$addToSet":{"comments":"andy"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.test.findOne()
{
"_id" : ObjectId("573957c55f74882a9bfa2d9e"),
"name" : "brent",
"age" : 30,
"email" : "xxxx@qq.com",
"phone" : 888,
"comments" : [
{
"name" : "jack",
"content" : "good"
},
{
"name" : "tom",
"content" : "very good"
},
"a",
"b",
"c",
"a",
"b",
"c",
"jack",
"andy"
]
}
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$addToSet":{"comments":{"$each":["andy","lily","lucy"]}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.findOne()
{
"_id" : ObjectId("573957c55f74882a9bfa2d9e"),
"name" : "brent",
"age" : 30,
"email" : "xxxx@qq.com",
"phone" : 888,
"comments" : [
{
"name" : "jack",
"content" : "good"
},
{
"name" : "tom",
"content" : "very good"
},
"a",
"b",
"c",
"a",
"b",
"c",
"jack",
"andy",
"lily",
"lucy"
]
}
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$pop":{"comments":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.findOne()
{
"_id" : ObjectId("573957c55f74882a9bfa2d9e"),
"name" : "brent",
"age" : 30,
"email" : "xxxx@qq.com",
"phone" : 888,
"comments" : [
{
"name" : "jack",
"content" : "good"
},
{
"name" : "tom",
"content" : "very good"
},
"a",
"b",
"c",
"a",
"b",
"c",
"jack",
"andy",
"lily"
]
}
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$pull":{"comments":"a"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.findOne()
{
"_id" : ObjectId("573957c55f74882a9bfa2d9e"),
"name" : "brent",
"age" : 30,
"email" : "xxxx@qq.com",
"phone" : 888,
"comments" : [
{
"name" : "jack",
"content" : "good"
},
{
"name" : "tom",
"content" : "very good"
},
"b",
"c",
"b",
"c",
"jack",
"andy",
"lily"
]
}
> db.test.findOne()
{
"_id" : ObjectId("573957c55f74882a9bfa2d9e"),
"name" : "brent",
"age" : 30,
"email" : "xxxx@qq.com",
"phone" : 888,
"comments" : [
{
"name" : "tom",
"content" : "very good"
},
"jack",
"andy",
"lily"
]
}
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$set":{"comments.0.num":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
> db.test.findOne()
{
"_id" : ObjectId("573957c55f74882a9bfa2d9e"),
"name" : "brent",
"age" : 30,
"email" : "xxxx@qq.com",
"phone" : 888,
"comments" : [
{
"name" : "tom",
"content" : "very good",
"num" : 1
},
"jack",
"andy",
"lily"
]
}
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$inc":{"comments.0.num":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.findOne()
{
"_id" : ObjectId("573957c55f74882a9bfa2d9e"),
"name" : "brent",
"age" : 30,
"email" : "xxxx@qq.com",
"phone" : 888,
"comments" : [
{
"name" : "tom",
"content" : "very good",
"num" : 2
},
"jack",
"andy",
"lily"
]
}
> db.test.update({"comments.name":"tom"},{"$set":{"comments.$.name":"jack"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.findOne()
{
"_id" : ObjectId("573957c55f74882a9bfa2d9e"),
"name" : "brent",
"age" : 30,
"email" : "xxxx@qq.com",
"phone" : 888,
"comments" : [
{
"name" : "jack",
"content" : "very good",
"num" : 2
},
"jack",
"andy",
"lily"
]
}
> db.test.find()
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 28 }
> db.test.update({"name":"bob"},{"$inc":{"age":1}},true)
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("573c86d3017c5eb7d08aed6d")
})
> db.test.find()
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573c86d3017c5eb7d08aed6d"), "name" : "bob", "age" : 1 }
>
> db.test.find()
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573c86d3017c5eb7d08aed6d"), "name" : "bob", "age" : 1 }
> db.test.update({"name":"tom"},{"$setOnInsert":{"age":10}},true)
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("573c88fe017c5eb7d08aed6e")
})
> db.test.find()
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573c86d3017c5eb7d08aed6d"), "name" : "bob", "age" : 1 }
{ "_id" : ObjectId("573c88fe017c5eb7d08aed6e"), "name" : "tom", "age" : 10 }
> db.test.update({"name":"tom"},{"$setOnInsert":{"age":20}},true)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.test.find()
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573c86d3017c5eb7d08aed6d"), "name" : "bob", "age" : 1 }
{ "_id" : ObjectId("573c88fe017c5eb7d08aed6e"), "name" : "tom", "age" : 10 }
> var a= db.test.findOne()
> a
{
"_id" : ObjectId("573c858c323f7f2e2ccb0e17"),
"name" : "brent",
"age" : 28
}
> a.age=40
40
> a
{
"_id" : ObjectId("573c858c323f7f2e2ccb0e17"),
"name" : "brent",
"age" : 40
}
> db.test.save(a)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 40 }
{ "_id" : ObjectId("573c86d3017c5eb7d08aed6d"), "name" : "bob", "age" : 1 }
{ "_id" : ObjectId("573c88fe017c5eb7d08aed6e"), "name" : "tom", "age" : 10 }
> db.test.find({"name":"brent"} )
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 40 }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 28 }
> db.test.update({"name":"brent"},{"$inc":{"age":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.update({"name":"brent"},{"$inc":{"age":1}},false,true)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.test.find({"name":"brent"} )
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 42 }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 29 }
>
> db.test.update({"name":"brent"},{"$inc":{"age":1}},false,true)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.runCommand({getLastError:1})
{
"connectionId" : 1,
"updatedExisting" : true,
"n" : 2,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
}
> db.test.find()
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43, "status" : "done" }
{ "_id" : ObjectId("573c86d3017c5eb7d08aed6d"), "name" : "bob", "age" : 1, "status" : "ready" }
{ "_id" : ObjectId("573c88fe017c5eb7d08aed6e"), "name" : "tom", "age" : 10, "status" : "ready" }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30, "status" : "done" }
> ps=db.runCommand({"findAndModify":"test", #集合名
... "query":{"status":"ready"},
... "sort":{"age":-1},
... "update":{"$set":{"status":"running"}}}).value
> db.test.update({"_id":ps._id},{"$set":{"status":"done"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43, "status" : "done" }
{ "_id" : ObjectId("573c86d3017c5eb7d08aed6d"), "name" : "bob", "age" : 1, "status" : "ready" }
{ "_id" : ObjectId("573c88fe017c5eb7d08aed6e"), "name" : "tom", "age" : 10, "status" : "done" }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30, "status" : "done" }
标签:
原文地址:http://blog.csdn.net/su377486/article/details/51449688