Javascript 更新JSON数组(Mongoose)中croncrete JSON对象中的特定字段
我试图用mongoose更新JSON数组中具体JSON对象中的特定字段。 我的MongoDB包含:Javascript 更新JSON数组(Mongoose)中croncrete JSON对象中的特定字段,javascript,arrays,json,node.js,mongodb,Javascript,Arrays,Json,Node.js,Mongodb,我试图用mongoose更新JSON数组中具体JSON对象中的特定字段。 我的MongoDB包含: db.users.find({username:"xxx"}) { "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : [ { "token" : "9e37axxx", "username" : "xxx", "_id" : ObjectId("572a7cfafe95dec51d9cbf2d") }, { "tok
db.users.find({username:"xxx"})
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : [ { "token" : "9e37axxx", "username" : "xxx", "_id" : ObjectId("572a7cfafe95dec51d9cbf2d") }, { "token" : "4529yyy", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } ] }
我想获取与“username”:“yyy”
和“user.\u id”=“56cb877e73da764818ec5ded”
匹配的JSON对象,并将其标记更改为“token”:“4529zzz”
,使用mongoose,如下所示:
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : [ { "token" : "9e37axxx", "username" : "xxx", "_id" : ObjectId("572a7cfafe95dec51d9cbf2d") }, { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } ] }
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } }
db的模式为:
var userSchema = new Schema({
username : { type: String, required: true },
...
githubtoken: [ { username: {type: String, required: false },
token: {type: String, required: false} }]
});
更新方法:
userSchema.statics.updateuser = function updateuser (query, update, options) {
var promise = new Hope.Promise();
this.findAndUpdate(query, update, options,function(error, user) {
if (error) {
return promise.done(error, null);
}else {
return promise.done(null, user);
}
});
return promise;
};
在我的express.js服务中:
query = {$and: [{_id: userid}, {githubtoken: {username:username, token:oldToken}} ]};
update = {$set : {githubtoken:{username:username, token:token}}};
options = { new: true};
User.updateuser(query,update,options).then(function (error,user){
if(error){
return promise.done(error,null);
}else{
}});
但它不起作用,因为删除所有githubtoken数组并只推送新的githubtoken,如下所示:
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : [ { "token" : "9e37axxx", "username" : "xxx", "_id" : ObjectId("572a7cfafe95dec51d9cbf2d") }, { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } ] }
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } }
有什么想法吗?
非常感谢:D
尝试按以下方式更改查询:
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : [ { "token" : "9e37axxx", "username" : "xxx", "_id" : ObjectId("572a7cfafe95dec51d9cbf2d") }, { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } ] }
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } }
var查询={
{
_id:新的ObjectId(userid),
'githubtoken.username':用户名,
'githubtoken.token':oldToken
}
};
并更新如下:
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : [ { "token" : "9e37axxx", "username" : "xxx", "_id" : ObjectId("572a7cfafe95dec51d9cbf2d") }, { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } ] }
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } }
var更新={
$set:{
'githubtoken.$.username':用户名,
'githubtoken.$.token':标记
}
};
尝试按以下方式更改查询:
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : [ { "token" : "9e37axxx", "username" : "xxx", "_id" : ObjectId("572a7cfafe95dec51d9cbf2d") }, { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } ] }
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } }
var查询={
{
_id:新的ObjectId(userid),
'githubtoken.username':用户名,
'githubtoken.token':oldToken
}
};
并更新如下:
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : [ { "token" : "9e37axxx", "username" : "xxx", "_id" : ObjectId("572a7cfafe95dec51d9cbf2d") }, { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } ] }
{ "_id" : ObjectId("56cb877e73da764818ec5ded"),..., "githubtoken" : { "token" : "4529zzz", "username" : "yyy", "_id" : ObjectId("572a7d3cfe95dec51d9cbf2e") } }
var更新={
$set:{
'githubtoken.$.username':用户名,
'githubtoken.$.token':标记
}
};
您可以使用$
-位置运算符进行更新:
db.collection('users').update({
"_id": ObjectId("56cb877e73da764818ec5ded"),
"githubtoken.username": "xxx"
}, {
$set: {
"githubtoken.$.username": username,
"githubtoken.$.token": token
}
});
这应该能奏效。有关使用
$-positional操作符的更多详细信息,您可以使用$
-positional操作符进行更新:
db.collection('users').update({
"_id": ObjectId("56cb877e73da764818ec5ded"),
"githubtoken.username": "xxx"
}, {
$set: {
"githubtoken.$.username": username,
"githubtoken.$.token": token
}
});
这应该能奏效。有关使用$-位置运算符的更多详细信息
我在以下内容中找到了解决方案:
感谢您的帮助:D我在以下方面找到了解决方案:
感谢帮助:DMongoose return me错误:位置运算符未从查询中找到所需的匹配项。未展开的更新:githubtoken。$.token.Mongoose return me错误:位置运算符未从查询中找到所需的匹配项。未展开的更新:githubtoken。$.token.Mongoose return me错误:位置运算符未从查询中找到所需的匹配项。未展开的更新:githubtoken.$.token。查询中的某些userId、username和oldToken参数似乎不匹配。这使得更新失败。如果将upsert
参数设置为true
,则如果不存在匹配项,将创建一个新文档。此查询不起作用,我仅使用_id启动查询,并且它是匹配项,问题是$set。虽然有效,但结果与我的问题相同,因为find返回一个user,set$crushes me数组并产生pushMongoose return me错误:位置运算符没有从查询中找到所需的匹配项。未扩展的更新:githubtoken.$.token。查询中的某些userId、username和oldToken参数似乎不匹配。这使得更新失败。如果将upsert
参数设置为true
,则如果不存在匹配项,将创建一个新文档。此查询不起作用,我仅使用_id启动查询,并且它是匹配项,问题是$set。虽然有效,但结果与我的问题相同,因为find返回一个用户并设置$crushes me数组并进行推送耶,$elemMatch
将是我的下一个建议:)耶,$elemMatch
将是我的下一个建议:)