Convert ObjectID to String in mongo Aggregation

后端 未结 5 1180
我在风中等你
我在风中等你 2021-02-05 12:15

I\'m in this scenario right now: I have a collection X:

{
  _id:ObjectId(\'56edbb4d5f084a51131dd4c6\'),
  userRef:ObjectId(\'56edbb4d5f084a51131dd4c6\'),
  seria         


        
5条回答
  •  时光说笑
    2021-02-05 12:50

    I couldn't find a way to do what I wanted, so instead, I created a MapReduce function that, in the end, generated the keys the way I wanted to (concatenating other keys).

    At the end, it looked something like this:

    db.collection('myCollection').mapReduce(
        function() {
            emit(
                this.userRef.str + '-' + this.serialNumber , {
                    count: 1,
                    whateverValue1:this.value1,
                    whateverValue2:this.value2,
                    ...
                }
            )
        },
        function(key, values) {
           var reduce = {}
           .... my reduce function....
            return reduce
        }, {
            query: {
                ...filters_here....
            },
            out: 'name_of_output_collection'
        }
    );
    

提交回复
热议问题