How to do find using node and mongodb?

前端 未结 3 1207
清歌不尽
清歌不尽 2020-12-07 05:06

Below Data stored in my collection

[
{ \"_id\" : ObjectId(\"53d9feff55d6b4dd1171dd9e\"),\"name\":\"John\", \"rank\":1, \"year\":1998 ,\"class\":\"a\" ,\"tota         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 05:36

    Although a few people have answered, but I believe complete way to do it is:

    router.post('/users',function(req, res, next) {
        // req.body = [{ "name":"John", "rank":1, "year":1998 },{ "name":"Sherif", "rank":1, "year":1999 }]
    
        // mapping users to each array
        const query = { $or: req.body };
        // execute query
        User.find(query, function(err, result) {
            if (err) next(err);
            console.log(result);
            res.json(result);
        });
    });
    

提交回复
热议问题