Mongo:查询以获取满足多个条件的对象列表

时间:2020-03-29 22:14:29

标签: database mongodb mongodb-query

我在编写查询以从结构中获取一些数据时遇到问题。我必须使用多个条件进行查询。我的标准:ID,期限,价格,主题。 例如,使用以下命令查询我的结构:

条件:

 id: [ObjectId("234434")]
 term: "24"
 price: 80 // return only this data that have price lower than 80
 topics: ["topic1", "topic2", "topic3"]

这是我经过简化的数据库结构:

{
   "_id":{
      "$oid":"234434"
   },
   "name":"name1",
   "posts":[
      {
         "name":"post1",
         "price":{
            "$numberDouble":"17.5"
         },
         "term":"24",
         "topics":[
            {
               "name":"topic1",
               "desc":"desc"
            },
            {
               "name":"topic23423",
               "desc":"desc"
            }
         ],
         "extraPosts":[
            {
               "name":"extra post 1",
               "description":"desc extra ",
               "topics":[
                  {
                     "name":"topic2",
                     "desc":"desc"
                  },
                  {
                     "name":"topic566566",
                     "desc":"desc"
                  }
               ]
            },
            {
               "name":"extra post 2",
               "description":"desc extra ",
               "topics":[
                  {
                     "name":"topic3",
                     "desc":"desc"
                  },
                  {
                     "name":"topic56656566",
                     "desc":"desc"
                  }
               ]
            },
            {
               "name":"extra post 3",
               "description":"desc extra ",
               "topics":[
                  {
                     "name":"topic4324",
                     "desc":"desc"
                  },
                  {
                     "name":"topic56623456566",
                     "desc":"desc"
                  }
               ]
            }
         ]
      }
   ]
}

查询应返回所有带有搜索主题的帖子,如果没有帖子,则搜索extraPosts并返回包含搜索主题的主要和多余的帖子。 因此,首先通过按ID,按词条,按价格,最后按主题过滤来限制结构。 因此,在这种情况下,它应该返回:

{
   "_id":{
      "$oid":"234434"
   },
   "name":"name1",
   "posts":[
      {
         "name":"post1",
         "price":{
            "$numberDouble":"17.5"
         },
         "term":"24",
         "topics":[
            {
               "name":"topic1",
               "desc":"desc"
            },
            {
               "name":"topic23423",
               "desc":"desc"
            }
         ],
         "extraPosts":[
            {
               "name":"extra post 1",
               "description":"desc extra ",
               "topics":[
                  {
                     "name":"topic2",
                     "desc":"desc"
                  },
                  {
                     "name":"topic566566",
                     "desc":"desc"
                  }
               ]
            },
            {
               "name":"extra post 2",
               "description":"desc extra ",
               "topics":[
                  {
                     "name":"topic3",
                     "desc":"desc"
                  },
                  {
                     "name":"topic56656566",
                     "desc":"desc"
                  }
               ]
            }
         ]
      }
   ]
}

因为post1具有topic1,并且在extraPosts中搜索了所有主题:Extra post 1,extra post2。它不应该在post1中返回,extraPost的名称为:extra post 3,因为它不包含搜索到的主题之一。如果所有主题均不在发布中,并且不在他的其他帖子中,则不返回此帖子。如果所有topcs都在额外的帖子1和额外的帖子1中,并且在post1中没有搜索到的主题,它还应该返回带有extraPosts的post1:额外的帖子1,额外的帖子2。 在我的实际结构中,有许多带有id的对象和许多帖子。

0 个答案:

没有答案
相关问题