标签:
find() 方法以非结构化的方式来显示所有文档。
如果你需要以易读的方式来读取数据,可以使用 pretty() 方法,语法格式如下:db.collection_name.find().pretty()
MongoDB数据更新可以使用update()函数。在执行remove()函数前先执行find()命令来判断执行的条件是否正确,这是一个比较好的习惯。
> db.user.save({name:"xxxxx",password:"11111"})
WriteResult({ "nInserted" : 1 })
> db.user.find().pretty()
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin"
}
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456"
}
{
"_id" : ObjectId("569476a63a18f4867aecbcd3"),
"name" : "xxxxx",
"password" : "11111"
}
> db.user.remove("name":"xxxxx")
2016-01-12T11:45:41.976+0800 E QUERY [thread1] SyntaxError: missing ) after a
rgument list @(shell):1:21
> db.user.remove({"name":"xxxxx"})
WriteResult({ "nRemoved" : 1 })
>还是没有注意检查括号
> db.user.find().pretty()
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin"
}
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456"
}
>
如果你想删除所有数据,可以使用以下方式(类似常规 SQL 的 truncate 命令):
db.user.remove()
如果你熟悉常规的 SQL 数据,通过下表可以更好的理解 MongoDB 的条件语句查询:
操作 | 格式 | 范例 | RDBMS中的类似语句 |
---|---|---|---|
等于 | {<key>:<value> } |
db.col.find({"by":"菜鸟教程"}).pretty() |
where by = ‘菜鸟教程‘ |
小于 | {<key>:{$lt:<value>}} |
db.col.find({"likes":{$lt:50}}).pretty() |
where likes < 50 |
小于或等于 | {<key>:{$lte:<value>}} |
db.col.find({"likes":{$lte:50}}).pretty() |
where likes <= 50 |
大于 | {<key>:{$gt:<value>}} |
db.col.find({"likes":{$gt:50}}).pretty() |
where likes > 50 |
大于或等于 | {<key>:{$gte:<value>}} |
db.col.find({"likes":{$gte:50}}).pretty() |
where likes >= 50 |
不等于 | {<key>:{$ne:<value>}} |
db.col.find({"likes":{$ne:50}}).pretty() |
where likes != 50 |
> db.user.update({"name":"admin"},{$set:{"grade":50}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.update({"name":"aaaaaaaaa"},{$set:{"grade":50}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find().pretty()
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin",
"grade" : 50
}
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456",
"grade" : 50
}
> db.user.update({"name":"aaaaaaaaa"},{$set:{"grade":99}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find().pretty()
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin",
"grade" : 50
}
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456",
"grade" : 99
}依旧是很粗心的毛病,导致后边的{"name":"aaaaaaaaa"}先更新成绩到50,然后又更新成绩到99,本来是第一次就打算更新到99的。
> db.user.find({"grade":{$gt:50}}).pretty()
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456",
"grade" : 99
}
> db.user.find({"grade":{$gte:50}}).pretty()
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin",
"grade" : 50
}
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456",
"grade" : 99
}
> db.user.find({"grade":{$ne:99}}).pretty()
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin",
"grade" : 50
}
> db.user.find({"grade":{$lte:100}}).pretty()
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin",
"grade" : 50
}
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456",
"grade" : 99
}
乱七八糟这么多,有用的就那么几句话,见谅见谅!
学习mongo系列(四) find().pretty() remove()
标签:
原文地址:http://www.cnblogs.com/mxh1099/p/5123879.html