仅获取 mongodb 集合中具有条件的第一个匹配文档

Posted

技术标签:

【中文标题】仅获取 mongodb 集合中具有条件的第一个匹配文档【英文标题】:Fetching only the first matched document with condition in mongodb collection 【发布时间】:2017-05-09 02:54:53 【问题描述】:

这是我的收藏

[
    
        _id: "585be0da13385513689f704a",
        site: "NBT",
        url: "m.nbt.com",
        label: "NBT Home",
        platform: "mobile",
        speed: 61,
        date: "2016-12-22T14:18:57.975Z"
    ,
    
        _id: "585be0da13385513689f704b",
        site: "NBT",
        url: "nbt.com",
        label: "NBT Home",
        platform: "desktop",
        speed: 75,
        date: "2016-12-22T14:15:57.975Z"
    ,
    
        _id: "585be0da13385513689f704c",
        site: "MT",
        url: "m.mt.com",
        label: "MT Home",
        platform: "mobile",
        speed: 40,
        date: "2016-12-22T14:01:57.975Z"
    ,
    
        _id: "585be0da13385513689f704d",
        site: "NBT",
        url: "m.nbt.com",
        label: "NBT Home",
        platform: "mobile",
        speed: 90,
        date: "2016-12-22T12:18:57.975Z"
    
]

预期输出:

[
    
        _id: "585be0da13385513689f704b",
        site: "NBT",
        url: "nbt.com",
        label: "NBT Home",
        platform: "desktop",
        speed: 75,
        date: "2016-12-22T14:15:57.975Z"
    ,
    
        _id: "585be0da13385513689f704c",
        site: "MT",
        url: "m.mt.com",
        label: "MT Home",
        platform: "mobile",
        speed: 40,
        date: "2016-12-22T14:01:57.975Z"
    ,
    
        _id: "585be0da13385513689f704d",
        site: "NBT",
        url: "m.nbt.com",
        label: "NBT Home",
        platform: "mobile",
        speed: 90,
        date: "2016-12-22T12:18:57.975Z"
    
]

基本上我想要第一个匹配的不同(标签+平台)组合文档。

正如您在 colloection 中看到的那样,标签-NBT Home 存在于 3 个文档中,但我只想检索标签 + 平台的唯一组合。所以 NBT Home for mobile 和 NBT Home for desktop 是我所期待的。

请帮忙。

【问题讨论】:

【参考方案1】:

试试这个:

db.collection.aggregate([
   
      $group:
         _id:
            label:"$label",
            platform:"$platform"
         ,
         site:
            $first:"$site"
         ,
         url:
            $first:"$url"
         ,
         speed:
            $first:"$speed"
         
      
   
])

输出:


    "_id" : 
        "label" : "MT Home",
        "platform" : "mobile"
    ,
    "site" : "MT",
    "url" : "m.mt.com",
    "speed" : 40


    "_id" : 
        "label" : "NBT Home",
        "platform" : "desktop"
    ,
    "site" : "NBT",
    "url" : "nbt.com",
    "speed" : 75


    "_id" : 
        "label" : "NBT Home",
        "platform" : "mobile"
    ,
    "site" : "NBT",
    "url" : "m.nbt.com",
    "speed" : 61

【讨论】:

这仅返回 2 条记录,但预期输出为 3。 MT Home 和 NBT Home+mobile 返回。 NBT Home + 桌面不见了。 @MohitRathi 再看看我发布的输出,返回了 3 个文档,并且存在 NBT 家庭 + 桌面。这是对您提供的示例文档进行聚合查询的结果...【参考方案2】:

在聚合管道中使用$group,然后使用$first或$limit

以下查询是使用 $first 编写的。

db.collection.aggregate([$group: "_id":"label":"$label", "platform":"$platform", "site": "$first":"$site", "url": "$first":"$url", "speed":"$first":"$speed", "date":"$first":"$date"  ])

对于具有以下记录的样本集合

db.collection.find()


 "_id" : "585be0da13385513689f704a", "site" : "NBT", "url" : "m.nbt.com", "labe
l" : "NBT Home", "platform" : "mobile", "speed" : 61, "date" : "2016-12-22T14:18
:57.975Z" 
 "_id" : "585be0da13385513689f704b", "site" : "NBT", "url" : "nbt.com", "label"
 : "NBT Home", "platform" : "desktop", "speed" : 75, "date" : "2016-12-22T14:15:
57.975Z" 
 "_id" : "585be0da13385513689f704c", "site" : "MT", "url" : "m.mt.com", "label"
 : "MT Home", "platform" : "mobile", "speed" : 40, "date" : "2016-12-22T14:01:57
.975Z" 
 "_id" : "585be0da13385513689f704d", "site" : "NBT", "url" : "m.nbt.com", "labe
l" : "NBT Home", "platform" : "mobile", "speed" : 90, "date" : "2016-12-22T12:18
:57.975Z" 

上面的查询将给出结果


  "_id": 
    "label": "MT Home",
    "platform": "mobile"
  ,
  "site": "MT",
  "url": "m.mt.com",
  "speed": 40,
  "date": "2016-12-22T14:01:57.975Z"


  "_id": 
    "label": "NBT Home",
    "platform": "mobile"
  ,
  "site": "NBT",
  "url": "m.nbt.com",
  "speed": 61,
  "date": "2016-12-22T14:18:57.975Z"


  "_id": 
    "label": "NBT Home",
    "platform": "desktop"
  ,
  "site": "NBT",
  "url ": "nbt.com",
  "speed": 75,
  "date": "2016-12-22T14:15:57.975Z"

【讨论】:

以上是关于仅获取 mongodb 集合中具有条件的第一个匹配文档的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB查询以选择具有所有元素都匹配某些条件的数组的文档

如何从 MongoDB 集合中获取具有匹配键的最后 N 个元素

如何从 MongoDB 集合中获取具有匹配键的最后 N 个元素

mongodb多个匹配条件并返回具有通用名称的文档

MongoDB - 仅投影数组中的匹配元素

MongoDB - 仅投影数组中的匹配元素