且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从猫鼬中的文档查询中仅获取子文档

更新时间:2023-02-19 16:11:57

您可以在查找查询中使用 selectprojection 来仅获取 items代码>.

You can use select or projection in your find query to get only items.

试试这个:

foodtr.find({},{'item_list':1},function(err,items){ 
    if (err) res.send(err); 
    res.json({ 
        status : '200',
        message:'popular items list', 
        data: items 
    }); 
});

{} 类似于查询条件,在这种情况下它将检索所有文档.{'item_list':1} 是告诉 mongo 只检索 item_list 数组(_id 与其他任何东西一起默认).

{} is similar to query condition, in this case it will retrieve all the documents. {'item_list':1} is to tell mongo to retrieve only item_list array(_id comes in default along with anything else).

希望这是你想要的.