MongoDB中嵌套文档的查询问题

发布于 2025-01-20 15:02:51 字数 1372 浏览 0 评论 0 原文

我想获取价格和日期信息并将两个特定日期和名称之间的信息发送到 ejs。

我的第一个文档是这样的,我有很多。例如,我想要 2022 年 4 月 1 日到 2022 年 4 月 6 日之间的所有比特币信息。没有嵌套它可以工作,但在这种情况下我不能这样做。

{
   "_id" : ObjectId("62175a6dd2b42d83134288f0"),
   "result" : [ 
       {
           "name" : "Bitcoin",
           "symbol" : "BTC",
           "price" : 45488.969940933,
           "date" : "2022-4-1",
           "time" : "16:25:22",
           "active" : 1
       }, 
       {
           "name" : "Ethereum",
           "symbol" : "ETH",
           "price" : 3328.61487109438,
           "date" : "2022-4-1",
           "time" : "16:25:22",
           "active" : 1
       }, 
   //..and goes to 100
      
   ]
}

myDocuments

我已经尝试过,

dbo.collection("info").find({ result : {name:"Bitcoin"}, date: { $gte: "2022-4-1", $lte: "2022-4-6" }.toArray //Getting an empty []
dbo.collection("info").find({ 'result.name' :"Bitcoin"}).toArray //Getting all of documents in the DB.
dbo.collection("info").find({"result": { $elemMatch:{"name": "Bitcoin"}}}).toArray //Same as above.

我也尝试过不使用 .toArray 和 findOne,但没有任何改变。非常感谢。

第一个对象的完整 JSON 为 https://pastebin.com/06V4upPY

I would like to get price and date info and send to ejs the between two specific dates and names.

My first document is like this and I have many. For instance I want all Bitcoin infos between 2022-4-1 and 2022-4-6. Without nested it works but in this case I couldn't do.

{
   "_id" : ObjectId("62175a6dd2b42d83134288f0"),
   "result" : [ 
       {
           "name" : "Bitcoin",
           "symbol" : "BTC",
           "price" : 45488.969940933,
           "date" : "2022-4-1",
           "time" : "16:25:22",
           "active" : 1
       }, 
       {
           "name" : "Ethereum",
           "symbol" : "ETH",
           "price" : 3328.61487109438,
           "date" : "2022-4-1",
           "time" : "16:25:22",
           "active" : 1
       }, 
   //..and goes to 100
      
   ]
}

myDocuments

I've tried

dbo.collection("info").find({ result : {name:"Bitcoin"}, date: { $gte: "2022-4-1", $lte: "2022-4-6" }.toArray //Getting an empty []
dbo.collection("info").find({ 'result.name' :"Bitcoin"}).toArray //Getting all of documents in the DB.
dbo.collection("info").find({"result": { $elemMatch:{"name": "Bitcoin"}}}).toArray //Same as above.

I have also tried without .toArray and findOne and nothing changed. Many thanks.

First object's full JSON is https://pastebin.com/06V4upPY

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风念你 2025-01-27 15:02:51
db.collection.find({
  result: {
    $elemMatch: {
      name: "Bitcoin",
      date: {
        $gte: "2022-4-1",
        $lte: "2022-4-6"
      }
    }
  }
},
{
  "result.$": 1
})

mongoplayground

db.collection.find({
  result: {
    $elemMatch: {
      name: "Bitcoin",
      date: {
        $gte: "2022-4-1",
        $lte: "2022-4-6"
      }
    }
  }
},
{
  "result.
quot;: 1
})

mongoplayground

奈何桥上唱咆哮 2025-01-27 15:02:51

正常的是,第二和第三查询返回所有文档,因为如果所有文档在结果数组中都有比特币元素,则它将返回文档。

$ Elemmatch运算符匹配包含数组字段的文档
至少有一个与所有指定查询匹配的元素
标准。

source $ elemmatch(query)doc

仅用于过滤器比特币元素您必须在投影部分中$ elemmatch
这样:

dbo.collection("info").find({ 
  'result.name' :"Bitcoin"
}, 
{
  'result': {
      $elemMatch: {
         "name": "Bitcoin" 
      }
   }
)

mongoplayground

$ lemmatch操作员限制了查询结果的字段内容

It's normal that the second and third query return all documents because if all documents have a Bitcoin element in the result array it will return the document.

The $elemMatch operator matches documents that contain an array field
with at least one element that matches all the specified query
criteria.

Source $elemMatch (query) doc

For filter only the bitcoin element you must $elemMatch in the projection part
Like this :

dbo.collection("info").find({ 
  'result.name' :"Bitcoin"
}, 
{
  'result': {
      $elemMatch: {
         "name": "Bitcoin" 
      }
   }
)

MongoPlayground

The $elemMatch operator limits the contents of an field from the query results

Source : $elemMatch (projection) doc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文