我正在尝试使用express框架在nodejs中的db集合中的所有现有文档中插入一个子文档。以下是代码片段:
updatedoc: function(update,options,cb)
{
return this.update({},update,options).exec(cb);
}
其中参数更新和选项如下:
const update = { $push: { "defaultads": content }};
const options = { multi: true};
它似乎运行并在控制台上给出以下输出:
{ n: 1, nmmodified: 1, 确定: 1 }
但在数据库的任何文档中根本没有发生推送。 我检查过 : 1)我是否插入正确的数据库。 2)是否传递了正确的值 但是我无法找到哪里出错了。
我是 Nodejs 新手,非常感谢解决此问题的指导。 提前致谢。
最佳答案
我给出了一个简单的代码,其中包含您的完整要求。首先使用此文件创建一个 config.js
,您将连接到 mongodb。这是代码
module.exports = {
'secretKey': '12345-67890-09876-54321',
'mongoUrl' : 'mongodb://localhost:27017/product'
}
接下来创建一个 models
文件夹。将此架构保留在此模型文件夹中。我将其命名为 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);
接下来创建一个 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
运行。
API
使用
GET
方法http://localhost:3000/route/product
。用于获取所有产品信息。使用
POST
方法http://localhost:3000/route/productData
。这用于创建文档。通过请求体以 json 格式发布数据,如{
“产品名称”:“糖果”, “产品价格”:“33”
}
您将看到这样的响应。
首先发布一些文档,即发布 2 或 3 个文档,然后您可以像我上面提到的那样使用 get API
查看文档。然后您将看到所有包含空子文档的文档。您将看到这样的响应
现在使用以下 api 将子文档添加到所有文档中。
使用
POST
方法http://localhost:3000/route/subdocument
。使用此功能,您可以向所有需要添加子文档的文档添加子文档{
“图像路径”:“桌面”
}
您可以看到这样的响应
在此之后,当您再次运行get API
时,您可以看到所有子文档都已添加到所有文档中。
希望这有帮助。
关于node.js - 无法使用nodejs在数据库的所有文档中插入子文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46088120/