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);

    });
有帮助吗?

解决方案

In case you need all URLs per tag:

db.link.aggregate({
  $group : {
    _id : "$tag",
    links: { $addToSet: "$url" }
  } 
})
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top