Go to my github

.NET 云原生架构师训练营(模块二 基础巩固 MongoDB 写入和查询)--学习笔记

2.5.3 MongoDB -- 写入和查询

  • 写入
  • 查询
  • 查找操作符
  • 逻辑操作符
  • 其他
  • 嵌套对象
  • 数组
  • 游标方法

写入

https://docs.mongodb.com/manual/tutorial/insert-documents/

  • insertOne
  • insertMany
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 无效

  • skip
  • limit
  • count
  • pretty 美化

课程链接

https://appsqsyiqlk5791.h5.xiaoeknow.com/v1/course/video/v_5f39bdb8e4b01187873136cf?type=2

知识共享许可协议

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。

如有任何疑问,请与我联系 (MingsonZheng@outlook.com) 。

posted @ 2021-01-02 21:43  郑子铭  阅读(295)  评论(0编辑  收藏  举报