我试着做以下几个问题.

  1. 查找2022年出版的图书的ISBN和书名.

  2. 查找ISBN、书名和至少有两位作者、最多有三位作者的图书的出版商.

MongoDB subject.js

对于第一个问题,我试过了: 我想试着使用$eq,但我不太确定如何使用,所以我try 使用$elemMatch,但似乎我也不知道它是如何工作的.

db.Subject.aggregate( [
     {"$unwind":"$subject.book"},
     {"$match":{"subject.book":{"$elemMatch":{"subject.yearPub":2022}} }},
     {"$project":{"subject.book.ISBN":1,"subject.book.bookTitle":1 }}
] )

对于第二个问题,我试过了: 到目前为止,这是有效的,它给了我三个作者的书.但由于某种原因,它错过了两位作者的书.

db.Subject.aggregate([{"$match": {"$or": [{'subject.book.author.3': {"$exists": true}},
{'book.author.2': {"$exists": true}}]}},
{"$project": {"subject.book.ISBN": 1,"subject.book.bookTitle": 1,"subject.book.publisher": 1,"_id":0}}
]).pretty()

推荐答案

问题1:使用点符号进行筛选:"subject.book.Year Pub"

db.Subject.aggregate([
  {
    "$unwind": "$subject.book"
  },
  {
    "$match": {
      "subject.book.yearPub": 2022
    }
  },
  {
    "$project": {
      "subject.book.ISBN": 1,
      "subject.book.bookTitle": 1
    }
  }
])

Demo Q1 @ Mongo Playground

Updated: To filter the document in the array个个

db.collection.aggregate([
  {
    "$match": {
      "subject.book.yearPub": 2022
    }
  },
  {
    $set: {
      "subject.book": {
        $filter: {
          input: "$subject.book",
          cond: {
            $eq: [
              "$$this.yearPub",
              2022
            ]
          }
        }
      }
    }
  },
  {
    "$project": {
      "subject.book.ISBN": 1,
      "subject.book.bookTitle": 1
    }
  }
])

Demo Q1.1 @ Mongo Playground


问题2:我怀疑你对结果的观察不正确.当索引从0开始时,subject.book.author.3将返回subject.book.author数组中的第四个元素.

对于您当前的查询,它将返回具有subject.book.author个数组中的第三个和第四个元素的文档,这意味着只要该数组包含more than 2个元素,就会返回该文档.

相反,您需要使用$size运算符来获取数组的大小.虽然有些文档没有book属性,因此使用$ifNull返回空数组,以防止在使用$size时出错.

db.Subject.aggregate([
  {
    "$match": {
      $expr: {
        $in: [
          {
            $size: {
              $ifNull: [
                "$subject.book.author",
                []
              ]
            }
          },
          [
            2,
            3
          ]
        ]
      }
    }
  },
  {
    "$project": {
      "subject.book.ISBN": 1,
      "subject.book.bookTitle": 1,
      "subject.book.publisher": 1,
      "_id": 0
    }
  }
])

Demo Q2 @ Mongo Playground

Updated: To filter the document in the array个个

db.collection.aggregate([
  {
    "$match": {
      $expr: {
        $in: [
          {
            $size: {
              $ifNull: [
                "$subject.book.author",
                []
              ]
            }
          },
          [
            2,
            3
          ]
        ]
      }
    }
  },
  {
    $set: {
      "subject.book": {
        $filter: {
          input: "$subject.book",
          cond: {
            $in: [
              {
                $size: {
                  $ifNull: [
                    "$subject.book.author",
                    []
                  ]
                }
              },
              [
                2,
                3
              ]
            ]
          }
        }
      }
    }
  },
  {
    "$project": {
      "subject.book.ISBN": 1,
      "subject.book.bookTitle": 1,
      "subject.book.publisher": 1,
      "_id": 0
    }
  }
])

Demo Q2.1 @ Mongo Playground


Note: You attach too many questions into one. Please remember to split it into different questions (posts) when you ask a new question in the future.

Mongodb相关问答推荐

字段$set聚合导致错误美元($)前缀字段$concatArrays对于存储无效"

MongoDB与合并对象聚合

获取响应周期中的特定键和值

在不知道字段名称的情况下如何引用 MongoDB 中的字段?

MongoDB 使用 pymongo 收集 500K 文档的写入速度很差

用Golang减go mongodb中的两个字段

删除一对一和一对多引用 - Mongoose

看起来当我执行 fs.writeFile() 时,更改的文件会重新启动 nodemon.怎么让它不重启?

更新 mongodb 文档中的特定字段

Mongodb将重音字符匹配为基础字符

字段类型在 MongoDB 索引中是否重要?

Mongoose Schema vs Mongo Validator

在 Nodejs 中配置最大旧空间大小

有没有办法将python对象直接存储在mongoDB中而不序列化它们

Django admin 和 MongoDB,可能吗?

我如何使用 Twitter 的流 api 中的推文并将它们存储在 mongodb 中

一起使用 MongoDB 和 Neo4j

未找到 MongoDB 数据/数据库

如何在 Mongoid 中引用嵌入的文档?

如何使用 mongodb-java-driver 进行 upsert