javascript - Mongo在插入后查找文档

标签 javascript node.js mongodb sails.js sails-mongo

我使用 sails js 0.12.14 和 mongodb。如果我在插入文档后尝试获取文档,则文档为空

Products.create(product).exec(function(err, createdProduct) {
    Products.find({_id : createdProduct.id}).exec(function(err, foundProductAfterCreate) {
        console.log(foundProductAfterCreate);
    })});

有人可以解释为什么该文档不可用吗?

更新: 这对我来说是正确的代码...

Products.create(product).exec(function(err, createdProduct) {
    Products.find({_id : createdProduct[0].id}).exec(function(err, foundProductAfterCreate) {
        console.log(foundProductAfterCreate);
    })});

最佳答案

您正在查询的文档实际上与您已有的文档相同

Products.create(product).exec(function(err, createdProduct) {
    // handle the error...
    console.log(createdProduct); // the full record
});

createdProduct 与通过 id 查询时得到的对象完全相同。

如果您确实需要通过 id 进行查询,sails 会从 mongo 标准的 _idid 进行相当全面的切换没有下划线。你会这样做:

Products.findOne({id: createdProduct[0].id}).exec(function(err, foundAgain) { // etc

...除非您使用 .native 进行更基本的 mongo 查询访问,否则任何地方都没有下划线。

关于javascript - Mongo在插入后查找文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47535699/

相关文章:

javascript - 如何使一个元素在悬停在另一元素上时悬停

javascript - 使用 json 填充 switch case 数据

node.js - 如何在 NestJS GraphQL 解析器中访问 Response 对象

c# - MongoDB .NET 驱动程序 : AsQueryable() is not found

javascript - 如何以 Angular 显示图像(服务器端上传文件夹中的图像上传和angularjs在不同的服务器上工作)?

javascript - 推送到 GH 页面时 IndexRoute 不显示

javascript - 有没有办法在 Typescript 中自动检查数据类型的数据相等性?

javascript - .url 文件使用什么标记语言?

node.js - 知道最新版本的 Node.js 与 MongoDB 的最新教程吗?

node.js - Mongoose 鉴别器如何在数据库中提供帮助?