db.collection.find(query, projection)
query,可选项,使用查询操作符指定查询条件
projection,可选项,使用投影操作符指定返回键值,若查询时返回文档中所有键值,则省略该参数
db.collection.find().pretty()
pretty()方法,格式化显示文档
db.collection.findOne()
仅返回第1个匹配文档
> db.col.find()
{ "_id" : ObjectId("5f1b9c103cb574c7da6ad4b9"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1ba0813cb574c7da6ad4ba"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1ba08b3cb574c7da6ad4bb"), "username" : "admin", "password" : "bar" }
> db.col.find({"username": "foo"})
{ "_id" : ObjectId("5f1b9c103cb574c7da6ad4b9"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1ba0813cb574c7da6ad4ba"), "username" : "foo", "password" : "bar" }
> db.col.findOne({"username": "foo"})
{
"_id" : ObjectId("5f1b9c103cb574c7da6ad4b9"),
"username" : "foo",
"password" : "bar"
}
> db.col.find({"username": "admin"}).pretty()
{
"_id" : ObjectId("5f1ba08b3cb574c7da6ad4bb"),
"username" : "admin",
"password" : "bar"
}
> db.col.find({"username": "foo"}, {"_id": 0})
{ "username" : "foo", "password" : "bar" }
{ "username" : "foo", "password" : "bar" }
> db.col.find({"username": "foo"}, {"username": 1}) // _id默认返回
{ "_id" : ObjectId("5f1b9c103cb574c7da6ad4b9"), "username" : "foo" }
{ "_id" : ObjectId("5f1b9c103cb574c7da6ad4ba"), "username" : "foo" }
条件操作符
{ <key>: <value> } // 等于
{ <key>: { $lt: <value> } } // 小于
{ <key>: { $lte: <value> } } // 小于或等于
{ <key>: { $gt: <value> } } // 大于
{ <key>: { $gte: <value> } } // 大于或等于
{ <key>: { $ne: <value> } } // 不等于
AND条件
db.collection.find({ key1: value1, key2: value2 })
> db.col.find({"username": "foo", "password": "bar"})
{ "_id" : ObjectId("5f1b9c103cb574c7da6ad4b9"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1ba0813cb574c7da6ad4ba"), "username" : "foo", "password" : "bar" }
OR条件
db.collection.find({ $or: [{ key1: value1 }, { key2: value2 }] })
> db.col.find({$or:[{"username": "foo"}, {"password": "bar"}]})
{ "_id" : ObjectId("5f1b9c103cb574c7da6ad4b9"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1ba0813cb574c7da6ad4ba"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1ba08b3cb574c7da6ad4bb"), "username" : "admin", "password" : "bar" }
$type操作符
基于BSON类型检索集合中匹配的数据类型,并返回结果
MongoDB支持类型有:double(1)、string(2)、object(3)、array(4)、binData(5)、objectId(7)、bool(8)、date(9)、null(10)、regex(11)、javascript(13)、symbol(14)、javascriptWithScope(15)、int(16, 32-bit integer)、timestamp(17)、long(18, 64-bit integer)、decimal(19, Decimal128, new in version 3.4)、minKey(-1)、maxKey(127)
> db.col.find({"username": {$type: "string"}})
{ "_id" : ObjectId("5f1b9c103cb574c7da6ad4b9"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1ba0813cb574c7da6ad4ba"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1ba08b3cb574c7da6ad4bb"), "username" : "admin", "password" : "bar" }
> db.col.find({"username": {$type: 2}})
{ "_id" : ObjectId("5f1b9c103cb574c7da6ad4b9"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1ba0813cb574c7da6ad4ba"), "username" : "foo", "password" : "bar" }
{ "_id" : ObjectId("5f1ba08b3cb574c7da6ad4bb"), "username" : "admin", "password" : "bar" }
limit()方法
db.collection.find().limit(number)
读取指定数量的文档
> db.col.find()
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6ccf"), "num" : 1, "name" : "Alpha" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd0"), "num" : 2, "name" : "Bravo" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd1"), "num" : 3, "name" : "Charlie" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd2"), "num" : 4, "name" : "Delta" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd3"), "num" : 5, "name" : "Echo" }
> db.col.find().limit(2)
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6ccf"), "num" : 1, "name" : "Alpha" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd0"), "num" : 2, "name" : "Bravo" }
skip()方法
db.collection.find().skip(number)
跳过指定数量的文档
> db.col.find()
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6ccf"), "num" : 1, "name" : "Alpha" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd0"), "num" : 2, "name" : "Bravo" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd1"), "num" : 3, "name" : "Charlie" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd2"), "num" : 4, "name" : "Delta" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd3"), "num" : 5, "name" : "Echo" }
> db.col.find().skip(2)
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd1"), "num" : 3, "name" : "Charlie" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd2"), "num" : 4, "name" : "Delta" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd3"), "num" : 5, "name" : "Echo" }
> db.col.find().limit(3).skip(1)
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd0"), "num" : 2, "name" : "Bravo" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd1"), "num" : 3, "name" : "Charlie" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd2"), "num" : 4, "name" : "Delta" }
> db.col.find().skip(1).limit(3)
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd0"), "num" : 2, "name" : "Bravo" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd1"), "num" : 3, "name" : "Charlie" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd2"), "num" : 4, "name" : "Delta" }
sort()方法
对数据进行排序
db.collection.find().sort({ key: number })
key指定排序字段,number指定排序方式,1为升序,-1为降序
> db.col.find().sort({"num": -1})
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd3"), "num" : 5, "name" : "Echo" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd2"), "num" : 4, "name" : "Delta" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd1"), "num" : 3, "name" : "Charlie" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd0"), "num" : 2, "name" : "Bravo" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6ccf"), "num" : 1, "name" : "Alpha" }
> db.col.find().sort({"num": -1}).skip(1).limit(3)
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd2"), "num" : 4, "name" : "Delta" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd1"), "num" : 3, "name" : "Charlie" }
{ "_id" : ObjectId("5f1bc98fed3b7aae0bdc6cd0"), "num" : 2, "name" : "Bravo" }