mongodb get distinct records

后端 未结 6 1215
栀梦
栀梦 2020-11-29 06:33

I am using mongoDB in which I have collection of following format.

{\"id\" : 1 , name : x  ttm : 23 , val : 5 }
{\"id\" : 1 , name : x  ttm : 34         


        
6条回答
  •  误落风尘
    2020-11-29 07:11

    If you want to write the distinct result in a file using javascript...this is how you do

    cursor = db.myColl.find({'fieldName':'fieldValue'})
    
    var Arr = new Array();
    var count = 0;
    
    cursor.forEach(
    
    function(x) {
    
        var temp = x.id;    
    var index = Arr.indexOf(temp);      
    if(index==-1)
       {
         printjson(x.id);
         Arr[count] = temp;
             count++;
       }
    })
    

提交回复
热议问题