https://docs.mongodb.com/manual/tutorial/insert-documents/
db.questions.insert(
{
"_id":"003",
"title":"第三个问题",
"view":0,
"isDeleted":false,
"content":"第三个问题",
"status":"open",
"tags":["c#"],
"answers":[
{"content":"回答1"},
{"content":"回答2"},
{"content":"回答3"}
]
}
)
db.questions.insertMany(
[
{
"_id":"004",
"title":"第三个问题",
"view":0,
"isDeleted":false,
"content":"第三个问题",
"status":"open",
"tags":["c#"],
"answers":[
{"content":"回答1"},
{"content":"回答2"},
{"content":"回答3"}
]
},
{
"_id":"005",
"title":"第三个问题",
"view":0,
"isDeleted":false,
"content":"第三个问题",
"status":"open",
"tags":["c#"],
"answers":[
{"content":"回答1"},
{"content":"回答2"},
{"content":"回答3"}
]
}
]
)
https://docs.mongodb.com/manual/reference/operator/query/
db.users.find(
{ age: { $gt: 18 } }, // 查询条件
{ name: 1, address: 1 } // 查询字段
).limit(5)
db.getCollection('questions').find({"title":"第三个问题"},{"title":1,"content":1})
db.getCollection('questions').find({},{"title":1,"content":1}).skip(1).limit(2)
Name | Description |
---|---|
$eq | 等于 |
$gt | 大于 |
$gte | 大于等于 |
$lt | 小于 |
$lte | 小于等于 |
$ne | 不等于 |
$in | 存在于 |
$nin | 不存在于:一般用于数组 |
// 大于等于
db.getCollection('questions').find({"view":{$gte: NumberInt(0)}})
// 存在于
db.getCollection('questions').find({"tags":{$in: ["c#"]}})
Name | Description |
---|---|
$and | 满足多个条件 |
$or | 满足多个条件中的一个 |
$not | 不匹配,或者字段不存在 |
$nor | 多个条件,一个都不满足 |
// 满足多个条件中的一个
db.getCollection('questions').find({$or:
[
{"tags":{$in: ["c#"]}},
{"view":{$gt:2}}
]
})
db.getCollection('questions').find({"view":{"$gt": 5}})
// 不匹配,或者字段不存在(取反)
db.getCollection('questions').find({"view": {$not: {"$gt": 5}}})
// 多个条件,一个都不满足
db.getCollection('questions').find({$nor: [{"view":{"$gt": 5}}]})
Name | Description |
---|---|
$exists | 存在某个字段 |
$type | 字段的类型 |
// 存在某个字段则显示
db.getCollection('questions').find({"best": {$exists:1}})
// 不存在某个字段则显示
db.getCollection('questions').find({"best": {$exists:0}})
// 字段的类型,16代表32-byte integer
db.getCollection('questions').find({"view": {$type: 16}})
https://mongoing.com/docs/reference/bson-types.html
db.getCollection('questions').find({"best.content":{$eq: "最好的答案"}})
Name | Description |
---|---|
$all | 所有元素匹配,匹配简单类型数组 |
$elemMatch | 用于匹配 object 数组 |
$size | 长度条件 |
db.getCollection('questions').find({"tags": {$in: ["c#"]}})
db.getCollection('questions').find({"tags": {$nin: ["c#"]}})
// 都必须包含
db.getCollection('questions').find({"tags": {$all: ["c#", "asp.net core"]}})
// 大小为2
db.getCollection('questions').find
// 包含 回答1 的数组
db.getCollection('questions').find({"answers": {$elemMatch: {"content": "回答1"}}})
db.getCollection('questions').find({"answers": {$elemMatch: {"content": {$gte: "回答1"}}}})
只在 mongo shell 中有效,其他语言版本 sdk 无效
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有