在MongoDB中查找最新的对象

huangapple go评论89阅读模式
英文:

Find Latest Object in MongoDB

问题

我有一些以以下形式存储在MongoDB中的数据:

{
    "_id" : 154,
    "record_id" : "001280000033x54AAA",
    "jsonData" : "",
    "user_id" : 1,
    "userName" : "abc@xyz.com",
    "backup_no" : 1
}

{
    "_id" : 155,
    "record_id" : "001280000033x54AAA",
    "jsonData" : "",
    "user_id" : 1,
    "userName" : "abc@xyz.com",
    "backup_no" : 2
}
...

我想根据'user_id'和'userName'检索数据,但是如果较低的'backup_no'中存在具有相同'record_id'的记录,则我需要选择具有最高'backup_no'的记录。

我尝试了对'record_id'进行聚合,然后查询,但是我找不到解决方案。

谢谢。

英文:

I have data in MongoDB in the form of :

 {
            "_id" : 154,
            "record_id" : "001280000033x54AAA",
            "jsonData" : "",
            "user_id" : 1,
            "userName" : "abc@xyz.com",
            "backup_no" : 1
 }
   

 {
            "_id" : 155,
            "record_id" : "001280000033x54AAA",
            "jsonData" : "",
            "user_id" : 1,
            "userName" : "abc@xyz.com",
            "backup_no" : 2
 }
  ...

I want to retrieve data based on 'user_id','userName', but if a record with same record_id exists in lower 'backup_no' then i need to choose record with highest backup_no.

I have tried to aggregate record_id's and then query but i am unable to find a solution.

Thanks

答案1

得分: 1

使用Mongo的聚合操作,你可以得到以下查询结果:

db.collectionName.aggregate({
  "$sort": {
    "backup_no": -1 //首先按backup_no排序
  }
}, {
  "$group": {
    "_id": "$record_id", //按record_id分组,只获取唯一的record_id
    "userId": {
      "$first": "$user_id"
    },
    "userName": {
      "$first": "$userName"
    },
    "backup_no": {
      "$first": "$backup_no"
    }
  }
}, {
  "$project": {
    "_id": 0,
    "record_id": "$_id",
    "user_id": "$user_id",
    "userName": "$userName",
    "backup_no": "$backup_no"
  }
})

希望对你有帮助!

英文:

Using mongo aggregation you can get your results check below query :

db.collectionName.aggregate({
  "$sort": {
	"backup_no": -1 //first sort by backup_no 
  }
}, {
  "$group": {
	"_id": "$record_id", // group by record_id so here only get distinct record_id
	"userId": {
	  "$first": "$user_id"
	},
	"userName": {
	  "$first": "$userName"
	},
	"backup_no": {
	  "$first": "$backup_no"
	}
  }
}, {
  "$project": {
	"_id": 0,
	"record_id": "$_id",
	"user_id": "$user_id",
	"userName": "$userName",
	"backup_no": "$backup_no"
  }
})

答案2

得分: 0

以下是翻译好的内容:

db.collection.aggregate([
  {
    $group: {
      _id: "$record_id",
      user_id: {
        $last: "$user_id"
      },
      userName: {
        $last: "$userName"
      },
      backup_no: {
        $max: "$backup_no"
      }
    }
  }
])

或者

你可以通过按backup_no降序排序,然后忽略除了每个不同record_id的第一条记录之外的所有记录。

db.users.find({}).sort({ backup_no: -1 });

MongoDB $orderby

英文:
db.collection.aggregate([{
  $group: {
    _id: "$record_id",
    user_id: {
      $last: "$user_id"
    },
    userName: {
      $last: "$userName"
    },
    backup_no: {
      $max: "$backup_no"
    }
  }
}])

or

You could simply sort descending by backup_no and then ignore all but the first record for each distinct record_id?
MongoDB $orderby

db.users.find( { } ).sort( { backup_no: -1 } );

huangapple
  • 本文由 发表于 2015年4月24日 21:24:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/29848716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定