gpt4 book ai didi

javascript - 将 JSON 对象推送到 Mongoose 数组中

转载 作者:太空宇宙 更新时间:2023-11-04 02:20:37 24 4
gpt4 key购买 nike

我对 Mongoose 还很陌生。我的目标是能够将数据推送到 Mongoose 数组中,这样我就可以稍后循环它:这是我的架构:

var moduleSchema = new mongoose.Schema({
name: {type: String, index: {unique: true}},
priority: Number,
author: String,
enabled: Boolean,
keywords: [{keyword: String}],
code: String
});

我希望能够将这种格式的 JavaScript 对象发送到 Express 并插入它自己的“关键字”字段。理想情况下,输出应如下所示:

{
"_id": "56401885bff833582442bb5f",
"name": "ModName",
"priority": 1,
"author": "tscrip",
"enabled": true,
"code": "TEST",
"__v": 0,
"keywords": [
{
"keyword": "Dog",
"keyword": "Cat"
}
]
}

每次我 PUT 数据来更新它时,我要么得到:

0:D,
1:O,
2:G

或者我得到:

The argument to $each in $push must be an array but it was of type Object

这是我更新模型的代码:

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);
}
);

我向它发送一个简单的 JSON 对象来更新:

{
"keywords": [
"mouse",
"bird"
]
}

任何帮助将不胜感激。

更新1

我试图将其变成一个数组:

var moduleSchema = new mongoose.Schema({
name: {type: String, index: {unique: true}},
priority: Number,
author: String,
enabled: Boolean,
keywords: [String],
code: String
});

并像这样更新它:

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);
}
);

但我现在收到错误:

{ [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\')}'

最佳答案

将对象推送到数组 Mongoose:

我的 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
}

下面是我用来将数据插入数组的代码。 Mongoose 带有内置的“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);
}
});

});
});

关于javascript - 将 JSON 对象推送到 Mongoose 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33610410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com