mongodb在mongodb中的项目中找到最大的键

时间:2017-08-14 08:10:46

标签: mongodb aggregation-framework

我有一个具有以下架构的mongodb集合

ALTER PROCEDURE [dbo].[SP_Hotel_Info_Update]
     -- Add the parameters for the stored procedure here
    @XHotelInfoDetails UpdateHotelTableType READONLY,

AS
BEGIN

    MERGE dbo.HotelInfo AS trg
    USING @XHotelInfoDetails AS src
      ON src.ID = trg.ID
     WHEN MATCHED THEN
       UPDATE SET FromDate = src.FromDate
     WHEN NOT MATCHED BY TARGET THEN
       INSERT (col1, col2, ...)
       VALUES (src.col1, src.col2, ...);
END

我想知道是否可以对数据对象内的项进行排序,或者使用核心mongodb选择最高值,而不是使用javascript检索和排序。

1 个答案:

答案 0 :(得分:0)

请先阅读@ NeilLunn对问题的评论

"可能"然而"不切实际"管道可能如下:

db.collection.aggregate([
    {$project: {
        data: { $objectToArray: "$data" }
    }}, 
    {$unwind: "$data"},
    {$sort: {_id:1, "data.k":-1}},
    {$group: {_id:"$_id", data:{$first:"$data"}}},
    {$group: {_id:"$_id", data:{$push:"$data"}}},
    {$project: {
        _id: 1,
        data: { $arrayToObject: "$data" }            
    }}
])