[英]Push JSON Object into Mongoose Array
I am fairly new to Mongoose. 我是猫鼬的新手。 My goal is to be able to push data into a Mongoose array, so I can loop through it at a later time: Here is my schema: 我的目标是能够将数据推送到Mongoose数组中,以便稍后可以遍历它:这是我的架构:
var moduleSchema = new mongoose.Schema({
name: {type: String, index: {unique: true}},
priority: Number,
author: String,
enabled: Boolean,
keywords: [{keyword: String}],
code: String
});
I would like to be able to send a JavaScript object in this format to the express and have it inserted with its own "keyword" field. 我希望能够以这种格式将JavaScript对象发送到express,并使用自己的“关键字”字段插入它。 Ideally, the output should look like this: 理想情况下,输出应如下所示:
{
"_id": "56401885bff833582442bb5f",
"name": "ModName",
"priority": 1,
"author": "tscrip",
"enabled": true,
"code": "TEST",
"__v": 0,
"keywords": [
{
"keyword": "Dog",
"keyword": "Cat"
}
]
}
Every time I PUT data to update this, I either get: 每次我输入数据来更新它时,我都会得到:
0:D,
1:O,
2:G
Or I get: 或者我得到:
The argument to $each in $push must be an array but it was of type Object
Here is my code to update the model: 这是我更新模型的代码:
if(req.body.keywords) updateObj.keywords = JSON.parse(req.body.keywords);
var keywordsArr = updateObj.keywords;
Module.findByIdAndUpdate({
"_id": req.params.id
},
{ $set: {keywords: { $each: { keyword: {keywordsArr}}}}},
function(err, numAffected){
console.log(err);
}
);
And I am sending it a simple JSON object to update: 我向它发送了一个简单的JSON对象进行更新:
{
"keywords": [
"mouse",
"bird"
]
}
Any help would be greatly appreciated. 任何帮助将不胜感激。
UPDATE 1 更新1
I attempted to just make it an array: 我试图将其设置为数组:
var moduleSchema = new mongoose.Schema({
name: {type: String, index: {unique: true}},
priority: Number,
author: String,
enabled: Boolean,
keywords: [String],
code: String
});
And update it like so: 像这样更新它:
if(req.body.keywords) updateObj.keywords = JSON.parse(req.body.keywords);
var keywordsArr = updateObj.keywords.keywords;
console.log(keywordsArr);
Module.findByIdAndUpdate({
"_id": req.params.id
},
{ $push: {keywords: {$each: keywordsArr}}},
function(err, numAffected){
console.log(err);
}
);
But I now get an error: 但是我现在得到一个错误:
{ [MongoError: exception: The field 'keywords' must be an array but is of type Object in document {_id: ObjectId('56401885bff833582442bb5f')}]
name: 'MongoError',
message: 'exception: The field \'keywords\' must be an array but is of type Object in document {_id: ObjectId(\'56401885bff833582442bb5f\')}'
Pushing Object to an array Mongoose: 将对象推入数组Mongoose:
My MongoDB or Mongoose Schema is like this: 我的MongoDB或Mongoose模式如下:
{
"_id": {
"$oid": "5511241323214"
},
"target": "200",
"region": "United Kingdom",
"fullname": "John Smith",
"milesLog": [
{
"miles": "1",
"activity": "Walking",
"month": "Apirl 2015",
"_id": {
"$oid": "5527d"
},
"date": {
"$date": "2015-04-10T13:53:43.549Z"
}
},
{
"month": "March 2015",
"activity": "Walking",
"miles": "1",
"_id": {
"$oid": "5527d9"
},
"date": {
"$date": "2015-04-10T14:07:37.004Z"
}
}
]
"__v": 0
}
And below is the code I use to push data into the array. 下面是我用来将数据推入数组的代码。 Mongoose comes with a built in "findByIdAndUpdate" function. 猫鼬带有内置的“ findByIdAndUpdate”功能。
app.put('/user/:id', function(req, res) {
// Using mongoose in-built function "findbyID"
User.findById(req.params.id, function(err, provider) {
// If error show.
if (err)
{
res.send(err);
}
// Using mongoose "findbyIDandUpdate" fucntion to "PUT" data into the array
User.findByIdAndUpdate(
provider.id,
{$push: {"milesLog":
{
// Passing Data
miles: req.body.miles,
activity: req.body.activity,
month: req.body.month
}}},
{safe: true, upsert: true},
function(err, model)
{
console.log(err);
},
// Response to send to User data as JSON
res.json(provider)
);
// Saving Data into MongoDB
provider.save(function(err)
{
if (err)
{
res.send(err);
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.