如何使用Mongoose an MongoDB 2.6 插入到子文档中数组中的某个位置 [英] How to insert to the certain position in the array in the subdocument by using Mongoose an MongoDB 2.6

查看:32
本文介绍了如何使用Mongoose an MongoDB 2.6 插入到子文档中数组中的某个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回答我的问题.建议的解决方案非常适合简单的数组.如果数组在子文档中或数组的每个元素都是数组,则建议的解决方案不起作用.我的意思是这样的:

A great explanation of how to use new $position operator of Mongodb 2.6 by using Mongoose was given in the answer to my question. The suggested solution works perfect for simple arrays. If array is in subdocument or each element of array is array the suggested solution doesn't work. I mean something like this:

List.collection.update(
{/*....*/},
{ "$push": {
    "subdocument.list": {
      "$each": [ 1, 2, 3 ],
      "$position": 0 }
    }
},function(err,NumAffected) {
  console.log("done");

});

List.collection.update(
{/*....*/},
{ "$push": {
    "list1.$.list2": {
      "$each": [ 1, 2, 3 ],
      "$position": 0 }
    }
},function(err,NumAffected) {
  console.log("done");

});

推荐答案

不确定是什么问题:

db.list.insert({ "list": { "sub": [4] } })
db.list.update(
    {},
    { "$push": { "list.sub": { "$each": [1,2,3], "$position": 0 } } }
)

{ "list" : { "sub" : [ 1, 2, 3, 4 ] } }

所以它按预期工作.

另一个例子:

db.list.insert({
    outer: [
       {
           key: "a", 
           inner: [4]
       },
       {
           key: "b", 
           inner: [4]
       }
   ]
})

db.list.update(
    { "outer.key": "b" },
    { "$push": { 
        "outer.$.inner": { 
            "$each": [1,2,3], "$position": 0 
        }
    }}
)

再次符合预期:

{
    "outer" : [
            {
                    "key" : "a",
                    "inner" : [
                            4
                    ]
            },
            {
                    "key" : "b",
                    "inner" : [
                            1,
                            2,
                            3,
                            4
                    ]
            }
    ]
}

已经解释了与特定驱动程序的交互,因此数据中肯定有一些不同的东西,但如果是这样,那么这些陈述是无效的.

The interaction with specific drivers was already explained so there must be something different in the data, but if so then those statements are not valid.

使用猫鼬完全相同:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/nodetest');

var listSchema = new Schema({

});

var List = mongoose.model( "List", listSchema );
var Other = mongoose.model( "Other", listSchema );

  List.collection.update(
    {},
    { "$push": {
    "list.sub": {
      "$each": [ 1, 2, 3 ],
      "$position": 0 }
    }
    },function(err,NumAffected) {
      console.log("done");

    }
  );


  Other.collection.update(
    { "outer.key": "b" },
    { "$push": {
        "outer.$.inner": {
            "$each": [ 1, 2, 3 ],
            "$position": 0
        }
    }},function(err,NumAffected) {
      console.log("done2")
    }
  );

这篇关于如何使用Mongoose an MongoDB 2.6 插入到子文档中数组中的某个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆