Question

Excuse my ignorance but it is possible to get distinct tag attribute with all the links in one query? I know you can using .distinct('tag') but then I need to send the data twice which require querying the same link again

I have the following schema

var Link = new Schema({

   title:{type:String, required:true},
   url:{type:String, required:true},
   tag:[{type: String,required:true}],
});

     link
    .find({})
    .exec(function(err,q){

       res.json(q);

    });
Was it helpful?

Solution

In case you need all URLs per tag:

db.link.aggregate({
  $group : {
    _id : "$tag",
    links: { $addToSet: "$url" }
  } 
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top