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:
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. 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:
{
"keywords": [
"mouse",
"bird"
]
}
Any help would be greatly appreciated.
UPDATE 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:
My MongoDB or Mongoose Schema is like this:
{
"_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.
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);
}
});
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.