无法使用nodejs在db中的所有文档中插入子文档

我正在试图插入一个子文档到所有现有的文档中的一个集合在数据库中,在nodejs中使用快递框架。以下是代码片段:

updatedoc: function(update,options,cb) { return this.update({},update,options).exec(cb); } 

参数更新和选项如下:

 const update = { $push: { "defaultads": content }}; const options = { multi: true}; 

它似乎运行,并在控制台上提供以下输出: { n: 1, nmodified: 1, ok: 1 }

但在数据库的任何文件中完全没有推送。 我已经检查:1)我是否在正确的分贝。 2)是否正确的值通过但是我无法find我要去哪里错了。

我是nodejs的新手,非常感谢解决这个问题的指导。 提前致谢。

我正在给你一个完整的要求简单的代码。 首先使用这个文件创build一个config.js你将连接到mongodb.Here是代码

 module.exports = { 'secretKey': '12345-67890-09876-54321', 'mongoUrl' : 'mongodb://localhost:27017/product' } 

接下来创buildmodels文件夹。 将此架构保留在此模型文件夹中。 我把它命名为product.js 。 这是代码

 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var imageSchema = new Schema({ imagepath:{ type:String } }); var nameSchema = new mongoose.Schema({ productName:{type: String}, productPrice:{type: Number}, imagePaths:[imageSchema] }); module.exports = mongoose.model("product", nameSchema); 

接下来创build一个routes文件夹,并将这个路由代码保存在这个文件夹中,我把它命名为route.js 。 这是代码

 var express = require('express'); var bodyParser = require('body-parser'); var mongoose = require('mongoose'); var Product = require('../models/product'); var app = express(); var Router = express.Router(); Router.use(bodyParser.json()); Router.get('/product',function(req,res){ Product.find({}, function (err, product) { if (err) throw err; res.json(product); }); }) Router.post('/productData',function(req, res, next){ Product.create(req.body, function (err, product) { if (err) throw err; console.log('Product Data created!'); var id = product._id; res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Added the product data with id: ' + id); }); }) Router.post('/subdocument',function (req, res, next) { Product.find({},function (err, result) { if (err) throw err; for(var i=0;i<result.length;i++){ result[i].imagePaths.push(req.body); result[i].save(function (err, ans) { if (err) throw err; console.log('SubDocument created!'); }); } res.send("Successfully added"); }); }) module.exports = Router; 

下一个服务器代码,我把它命名为app.js 这是代码

 var express = require('express'); var bodyParser = require('body-parser'); var Product = require('./models/product'); var mongoose = require('mongoose'); var config = require('./config'); mongoose.connect(config.mongoUrl); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function () { console.log("Connected correctly to server"); }); var app = express(); var route=require('./routes/route'); app.use('/route',route); app.listen(3000,function(){ console.log("Server listening on 3000"); }); 

作为node app.js运行服务器。

蜜蜂

  1. 使用GET方法http://localhost:3000/route/product 。这是为了获取所有的产品信息。

  2. 使用POST方法http://localhost:3000/route/productData 。这用于创build文档。 以json格式通过请求体发布数据

    {

    “productName”:“糖果”,“productPrice”:“33”

    }

你会看到这样的回应。

在这里输入图像说明 首先发布一些文件,即发表2或3文件,然后你可以看到文件与get API如上所述。 然后你会看到所有的文件都包含空的子文件。 你会看到这样的回应

在这里输入图像说明

现在使用下面的api添加子文档。

  1. 使用POST方法http://localhost:3000/route/subdocument 。 使用这个,你可以添加一个子文件到这样的所有文件,你需要添加子文件

    {

    “imagepath”:“桌面”

    }

你可以看到这样的回应

在这里输入图像说明

再次运行get API您可以看到所有的子文档都被添加到所有文档中。

在这里输入图像说明

希望这可以帮助。