MongoDB学习系列2:shell中如何查询文档,修改文档,删除文档

查询使用find()函数,返回集合里所有的文档。

如果只是查看一个,使用findOne

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

db.myblogs.findOne()
{
	"_id" : ObjectId("52ad2a49af25ec9b0c6a42db"),
	"title" : "my blog title",
	"content" : "hello,everyone",
	"data" : "2013-12-15 12:05"
}
> db.myblogs.find()
{ "_id" : ObjectId("52ad2a49af25ec9b0c6a42db"), "title" : "my blog title", "content" : "hello,everyone", "data" : "2013-12-15 12:05" }
{ "_id" : ObjectId("52ad2dbeaf25ec9b0c6a42dc"), "title" : "my blog title", "content" : "hello,everyone", "data" : "2013-12-15 12:05" }

~~~~~~~~~~~~~~~~~~~~~~关于修改

修改使用update函数,至少要带2个参数,

1是更新文档的限制条件

2 新的文档

post={"name":"lzq","age":100,"address":"jiangsu nanjing"}
{ "name" : "lzq", "age" : 100, "address" : "jiangsu nanjing" }

然后插入

db.myblogs.insert(post)
> db.myblogs.find()
{ "_id" : ObjectId("52ad2a49af25ec9b0c6a42db"), "title" : "my blog title", "content" : "hello,everyone", "data" : "2013-12-15 12:05" }
{ "_id" : ObjectId("52ad2dbeaf25ec9b0c6a42dc"), "title" : "my blog title", "content" : "hello,everyone", "data" : "2013-12-15 12:05" }
{ "_id" : ObjectId("52ad304eaf25ec9b0c6a42dd"), "name" : "lzq", "age" : 100, "address" : "jiangsu nanjing" }

修改本地的POST

post.company="china,tech"
china,tech
> post
{
	"name" : "lzq",
	"age" : 100,
	"address" : "jiangsu nanjing",
	"company" : "china,tech"
}

然后update到数据库中

db.myblogs.update({"_id":ObjectId("52ad304eaf25ec9b0c6a42dd")},post)
> db.myblogs.find()
{ "_id" : ObjectId("52ad2a49af25ec9b0c6a42db"), "title" : "my blog title", "content" : "hello,everyone", "data" : "2013-12-15 12:05" }
{ "_id" : ObjectId("52ad2dbeaf25ec9b0c6a42dc"), "title" : "my blog title", "content" : "hello,everyone", "data" : "2013-12-15 12:05" }
{ "_id" : ObjectId("52ad304eaf25ec9b0c6a42dd"), "name" : "lzq", "age" : 100, "address" : "jiangsu nanjing", "company" : "china,tech" }

效果已经出来了。

~~~~~~~~~~~~~~~~~~关于删除

remove函数用来永久性的删除文档。

如果不加参数,就是删除集合内所有文档。

db.myblogs.remove()
> db.myblogs.findOne()
null
> db.myblogs.find()

 

 

 

 

 

你可能感兴趣的:(mongodb,mongodb,mongodb)