MongoDB 匹配查询和比较操作符

一、匹配查询

1. 查询所有

> db.accounts.find()
{ "_id" : "account1", "name" : "alice", "balance" : 100 }
{ "_id" : ObjectId("5d7f25f4aecbd2bc0fa821b3"), "name" : "charlie", "balance" : 500 }
{ "_id" : ObjectId("5d7f25f4aecbd2bc0fa821b4"), "name" : "david", "balance" : 200 }
{ "_id" : { "name" : "david", "balance" : 200 }, "name" : "charlie", "balance" : 500 }
{ "_id" : { "balance" : 200, "name" : "david" }, "name" : "charlie", "balance" : 500 }
{ "_id" : { "balance" : "200", "name" : "david" }, "name" : "charlie", "balance" : 500 }

2. 查询所有+展示美化

> db.accounts.find().pretty()

3. 查询特定字段

> db.accounts.find({"name":"alice2"}).pretty()
{ "_id" : "account1", "name" : "alice2", "balance" : 100 }
{
 "_id" : ObjectId("5d7f43abaecbd2bc0fa821b7"),
 "name" : "alice2",
 "balance" : 100
}

4. 查询主键

> db.accounts.find({"_id.name":"david"}).pretty()
{
 "_id" : {
  "name" : "david",
  "balance" : 200
 },
 "name" : "charlie",
 "balance" : 500
}

二、使用比较操作符

操作符说明
eq等于
ne不等于
gt大于
gte大于等于
lt小于
lte小于等于
> db.accounts.find({"balance":{$lt:200}}).pretty()
{ "_id" : "account1", "name" : "alice2", "balance" : 100 }
{
 "_id" : ObjectId("5d7f43abaecbd2bc0fa821b7"),
 "name" : "alice2",
 "balance" : 100
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值