use mongoose model.find() to get all entries of only 1 field

后端 未结 2 1918
孤独总比滥情好
孤独总比滥情好 2021-01-26 16:00

I\'ve tried to use different variation of model.find(), but none do what I want.

code below is what I\'m working with, but it displays every single field, and I only wan

2条回答
  •  长情又很酷
    2021-01-26 17:00

    What you are looking for is called projection:

    Video.find({}, {iframe: 1}, function (err, docs) {
       res.json(docs);
    });
    

    The second parameter to the find function tells which field to return. If you do not want the _id as well, then use: {_id:0, iframe:1}

    Like so:

    Video.find({}, {_id:0, iframe:1}, function (err, docs) {
       res.json(docs);
    });
    

    However, projection does not give you distinct values. It only returns the fields you want to use (along with repetitions).

提交回复
热议问题