Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何覆盖mongodb(mongoose)中子文档的数组属性_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript 如何覆盖mongodb(mongoose)中子文档的数组属性

Javascript 如何覆盖mongodb(mongoose)中子文档的数组属性,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我有一个模式 { name: {type:String} ..... child : {type: [childSchema], []} } 和子模式 { x:{type:Number} y:{type:Number}, options: {type:Array, default} } 问题是,虽然我可以使用特定的子id更新单个子属性,但我无法更新/替换选项数组(只是字符串数组),我必须 我也试过了 $set: { 'child.$.

我有一个模式

{

   name: {type:String}
   .....
   child : {type: [childSchema], []}

}
和子模式

{
   x:{type:Number}
   y:{type:Number},
   options: {type:Array, default}
}
问题是,虽然我可以使用特定的子id更新单个子属性,但我无法更新/替换选项数组(只是字符串数组),我必须

我也试过了

$set: {
         'child.$.x' : newX,
         'child.$.y' : newy,
         'child.$.options' : { '$all' ['option1', 'option2']}
  }
我认为(但我不确定)可能我不能在这个级别使用任何$functions($set,$all)

当我在谷歌上搜索时,我似乎找到了更多关于更新子文档的链接,并且可以找到任何关于替换子文档中数组的内容,我尝试了查找Mongodb&mongoose API,但除非我忽略了一些内容,否则我找不到任何在这种情况下有效的内容


有人能给我指出正确的方向吗

试试mongo shell中的更新,如以下示例所示:

> db.test.drop()
> db.test.insert({
    "_id" : 0,
    "children" : [
        { "_id" : 1, "x" : 1, "y" : 2, "options" : [1, 2, 3] },
        { "_id" : 2, "x" : 5, "y" : 8, "options" : [1, 6, 2] }
    ]
})
> db.test.update({ "_id" : 0, "children._id" : 1 },
    { "$set" : { "children.$.x" : 55, "children.$.y" : 22, "children.$.options" : [9, 8, 7] } }
)
> db.test.findOne()
{
    "_id" : 0,
    "children" : [
        { "_id" : 1, "x" : 55, "y" : 22, "options" : [9, 8, 7] },
        { "_id" : 2, "x" : 5, "y" : 8, "options" : [1, 6, 2] }
    ]
}

您是否尝试过不使用
$
即-child.options{}
> db.test.drop()
> db.test.insert({
    "_id" : 0,
    "children" : [
        { "_id" : 1, "x" : 1, "y" : 2, "options" : [1, 2, 3] },
        { "_id" : 2, "x" : 5, "y" : 8, "options" : [1, 6, 2] }
    ]
})
> db.test.update({ "_id" : 0, "children._id" : 1 },
    { "$set" : { "children.$.x" : 55, "children.$.y" : 22, "children.$.options" : [9, 8, 7] } }
)
> db.test.findOne()
{
    "_id" : 0,
    "children" : [
        { "_id" : 1, "x" : 55, "y" : 22, "options" : [9, 8, 7] },
        { "_id" : 2, "x" : 5, "y" : 8, "options" : [1, 6, 2] }
    ]
}