MongoDB 插入、更新、删除
向 集合 添加新文档
从 集合 里删除文档
更新 现有文档
1. 插入并保存文档
> db.foo.insert({"bar":"baz"})
这个操作会给文档增加一个"_id"键 (如果 原来没有这个键的话),然后保存到MongoDB中
> db.foo.find()
{ "_id" : ObjectId("4fdc779a89834bfb0bd267a3"), "bar" : "baz" }
{ "_id" : ObjectId("4fdc779a89834bfb0bd267a3"), "bar" : "baz" }
文档不能超过 4 MB , 可以 用 Object.bsonsize( 文档 ) 查看文档转换为 BSON 的大小 (以 字节 为 单位)
> post = { "title":"My Blog post","content":"Here's my blog post.","date":
... new Date()}
{
"title" : "My Blog post",
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z")
}
> Object.bsonsize(post)
77
... new Date()}
{
"title" : "My Blog post",
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z")
}
> Object.bsonsize(post)
77
2. 删除文档
db.foo.remove() 会删除foo集合中所有的文档,当不会删除集合本身,原来的索引也会保留。
> db.foo.findOne()
{ "_id" : ObjectId("4fdc779a89834bfb0bd267a3"), "bar" : "baz" }
> show collections
foo
system.indexes
> db.foo.remove()
> show collections
foo
system.indexes
> db.foo.find()
>
{ "_id" : ObjectId("4fdc779a89834bfb0bd267a3"), "bar" : "baz" }
> show collections
foo
system.indexes
> db.foo.remove()
> show collections
foo
system.indexes
> db.foo.find()
>
remove() 函数可以接受 一个查询文档 作为可选 参数, 这样只有符合条件的参数 才被删除。
> post2 = {"title":"My Blog post2","content":"Here's my blog post2.","date":
... new Date()}
{
"title" : "My Blog post2",
"content" : "Here's my blog post2.",
"date" : ISODate("2012-06-16T12:41:34.429Z")
}
> db.foo.insert(post2)
> db.foo.find()
{ "_id" : ObjectId("4fdc7e1889834bfb0bd267a4"), "title" : "My Blog post", "conte
nt" : "Here's my blog post.", "date" : ISODate("2012-06-16T12:19:25.163Z") }
{ "_id" : ObjectId("4fdc7f2689834bfb0bd267a5"), "title" : "My Blog post2", "cont
ent" : "Here's my blog post2.", "date" : ISODate("2012-06-16T12:41:34.429Z") }
> db.foo.remove({"title":"My Blog post2"})
> db.foo.find()
{ "_id" : ObjectId("4fdc7e1889834bfb0bd267a4"), "title" : "My Blog post", "conte
nt" : "Here's my blog post.", "date" : ISODate("2012-06-16T12:19:25.163Z") }
>
... new Date()}
{
"title" : "My Blog post2",
"content" : "Here's my blog post2.",
"date" : ISODate("2012-06-16T12:41:34.429Z")
}
> db.foo.insert(post2)
> db.foo.find()
{ "_id" : ObjectId("4fdc7e1889834bfb0bd267a4"), "title" : "My Blog post", "conte
nt" : "Here's my blog post.", "date" : ISODate("2012-06-16T12:19:25.163Z") }
{ "_id" : ObjectId("4fdc7f2689834bfb0bd267a5"), "title" : "My Blog post2", "cont
ent" : "Here's my blog post2.", "date" : ISODate("2012-06-16T12:41:34.429Z") }
> db.foo.remove({"title":"My Blog post2"})
> db.foo.find()
{ "_id" : ObjectId("4fdc7e1889834bfb0bd267a4"), "title" : "My Blog post", "conte
nt" : "Here's my blog post.", "date" : ISODate("2012-06-16T12:19:25.163Z") }
>
remove 删除数据是永久性的,不能撤销,也不能恢复。
另一个 删除命令 db.runCommand({"drop":"foo"}) 。
3. 更新
最简单的方法 用一个新文档 替换 原文档。 这种 适用于 结构变化较大的时候。
> user = { "name" :"joe",
... "friends":32,
... "enemies":2
... }
{ "name" : "joe", "friends" : 32, "enemies" : 2 }
> db.user.insert(user)
> db.user.find()
{ "_id" : ObjectId("4fdc893d89834bfb0bd267a6"), "name" : "joe", "friends" : 32,
"enemies" : 2 }
... "friends":32,
... "enemies":2
... }
{ "name" : "joe", "friends" : 32, "enemies" : 2 }
> db.user.insert(user)
> db.user.find()
{ "_id" : ObjectId("4fdc893d89834bfb0bd267a6"), "name" : "joe", "friends" : 32,
"enemies" : 2 }
例如:
{
"_id" : ObjectId("4fdc893d89834bfb0bd267a6"),
"name" : "joe" ,
"friends" : 32 ,
"enemies" : 2
}
改变为
{
"_id" : ObjectId("4fdc893d89834bfb0bd267a6"),
"username" : "joe" ,
"relationships" :
{
"friends" : 32 ,
"enemies" : 2
}
}
可以用update 来替换文档:
> var joe = db.user.findOne({"name":"joe"})
> joe.relationships = { "friends":joe.friends , "enemies" : joe.enemies};
{ "friends" : 32, "enemies" : 2 }
> joe.username = joe.name
joe
> delete joe.name
true
> delete joe.friends
true
> delete joe.enemies
true
> db.user.find()
{ "_id" : ObjectId("4fdc893d89834bfb0bd267a6"), "name" : "joe", "friends" : 32,
"enemies" : 2 }
> db.user.update({"name":"joe"},joe);
> db.user.find()
{ "_id" : ObjectId("4fdc893d89834bfb0bd267a6"), "relationships" : { "friends" :
32, "enemies" : 2 }, "username" : "joe" }
>
> joe.relationships = { "friends":joe.friends , "enemies" : joe.enemies};
{ "friends" : 32, "enemies" : 2 }
> joe.username = joe.name
joe
> delete joe.name
true
> delete joe.friends
true
> delete joe.enemies
true
> db.user.find()
{ "_id" : ObjectId("4fdc893d89834bfb0bd267a6"), "name" : "joe", "friends" : 32,
"enemies" : 2 }
> db.user.update({"name":"joe"},joe);
> db.user.find()
{ "_id" : ObjectId("4fdc893d89834bfb0bd267a6"), "relationships" : { "friends" :
32, "enemies" : 2 }, "username" : "joe" }
>