简体   繁体   English

查找与 mongodb 中提供的所有参数匹配的文档

[英]Find document matching to all the provided parameters in mongodb

Suppose I have documents in collection bookDetails,假设我在集合 bookDetails 中有文档,

doc1: {'bookData':{'bookName':'lotr','bookPart':'Part 3'}, 'library_name':'omega','libraryId':12166}

doc2: {'bookData':{'bookName':'hp','bookPart':'Part 7'}, 'library_name':'omega','libraryId':12199}

doc3: {'bookData':{'bookName':'hertu','bookPart':'Part 7'}, 'library_name':'omega','libraryId':9999}

I want to fetch only document which matches with all fields library name: omega, library id: 12199 and bookName: hp.我只想获取与所有字段库名称匹配的文档:omega,库 ID:12199 和 bookName:hp。

For this I tried as:为此,我尝试如下:

await collection.aggregate([{ $match: { library_name: 'omega' } }, ]).exec();{ $match: { libraryId: 12199 } } await collection.aggregate([{ $match: { library_name: 'omega' } }, ]).exec();{ $match: { libraryId: 12199 } }

But I also want to match with bookname: 'hp', so I can get required document.但我也想匹配 bookname: 'hp',这样我就可以获得所需的文件。

If anyone needs any further information please let me know.如果有人需要任何进一步的信息,请告诉我。

Demo - https://mongoplayground.net/p/C2EP96CkIRo演示 - https://mongoplayground.net/p/C2EP96CkIRo

db.collection.find({
  library_name: "omega",
  libraryId: 12199,
  "bookData.bookName": "hp"
})

Demo - https://mongoplayground.net/p/9mf9Sp8C4u6演示 - https://mongoplayground.net/p/9mf9Sp8C4u6

With aggregation有聚合

db.collection.aggregate([
  {
    $match: {
      library_name: "omega",
      libraryId: 12199,
      "bookData.bookName": "hp"
    }
  }
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM