小编use*_*529的帖子

Mongo DB Document仅返回带有"$ oid"的"_id"

我正在尝试使用MEAN堆栈的非常简单的CRUD API.我在mongolab沙箱db中输入了几个文件.我可以对所有文件进行GET,他们都会被退回.然后我通过ID测试了GET:

router.route('/api/v1/resources/:resource_id')

// get the resource with that id (accessed at GET http://localhost:8080/api/resources/:resource_id)
.get(function(req, res) {
    Resource.findById(req.params.resource_id, function(err, resources) {
        if (err)
            res.send(err);
        res.json(resources);
    });
});
Run Code Online (Sandbox Code Playgroud)

它根本行不通.我一直变空.但文件存在.当我在所有文件上进行GET时,我可以看到它.

然后我终于得到了另一份文件.

区别?返回的记录具有以下格式的ID:

{
  "_id": { "$oid":"1234567890abc"}
}
Run Code Online (Sandbox Code Playgroud)

但是没有返回的记录有这样的:

{
   "_id": "1234567890abc"
}
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释一下吗?我用Postman输入了数据,我没有在条目之间做任何不同的事情.

什么是$ oid?什么创造了$ oid?为什么嵌套对于mongoose的findById()很重要?

谢谢!

mongoose mongodb node.js

4
推荐指数
1
解决办法
3735
查看次数

标签 统计

mongodb ×1

mongoose ×1

node.js ×1