Frage

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

    });
War es hilfreich?

Lösung

In case you need all URLs per tag:

db.link.aggregate({
  $group : {
    _id : "$tag",
    links: { $addToSet: "$url" }
  } 
})
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top