how to use mongodb aggregate and retrieve entire documents

后端 未结 5 2075
猫巷女王i
猫巷女王i 2021-01-11 10:53

I am seriously baffled by mongodb\'s aggregate function. All I want is to find the newest document in my collection. Let\'s say each record has a field \"created\"



        
5条回答
  •  耶瑟儿~
    2021-01-11 11:49

    query = [
        {
            '$sort': {
                'created': -1
            }
        },
        {
            $group: { 
                '_id':null,
                'max':{'$first':"$$ROOT"}
            }
        }
    ]
    db.collection.aggregate(query)
    

提交回复
热议问题