MongoDb:如何将其他对象插入到对象集合中? [英] MongoDb : How to insert additional object into object collection?

查看:308
本文介绍了MongoDb:如何将其他对象插入到对象集合中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份文件所有者,可以有n个营地,营地有教师,教师有班级。之前我尝试使用嵌套数组来完成此操作(请参阅下面链接到我的帖子),但是我了解到位置运算符$没有嵌套那么深。
MongoDB:将数组添加到现有数组中

I have a document "owner" that can have "n" number of camps and camps have "instructors" and instructors have "classes". Earlier I tried accomplishing this with nested arrays (see link to my post below), however I learned that the positional operator "$" does not nest that deep. MongoDB: Adding an array into an existing array

但是,我了解到解决方法是使用对象集合而不是数组。我假设我必须使用更新和$ set来添加额外的阵营,但是每次我做的都是覆盖(更新)之前插入的阵营。

However, I learned that the workaround would be to use object collections instead of arrays. I'm assuming that I have to use "update" with "$set" to add additional "camps", however every time I do all it does is overwrite(update) the previous camp that was inserted.

以下是我想要完成的层次结构:

Below is the hierarchical structure I am trying to accomplish:

owner = {

    firstName: 'john',
    lastName: 'smith',
    ownerEmail: 'john.smith@gmail.com',

    camps : {

        {
            name: 'cubs-killeen',
            location: 'killeen'
        },

        {
            name: 'cubs-temple',
            location: 'temple'
        },

        instructors : {

            {
                firstName: 'joe',
                lastName : 'black'
            },

            {
                firstName: 'will',
                lastName : 'smith'
            }        
        }

    }

}

我也有我一直在尝试以下方法:

I have also been trying the following:

db.owners.update({ownerEmail:'john.smith@gmail.com'}, {$set: { camps:{ {name:'cubs-killeen'} } }})

但是这会抛出一个意外的标识符{错误。

but this throws an unexpected identifier { error.

任何有关实现上述结构的示例mongo命令的帮助都将非常受欢迎。

Any help with some sample mongo commands to achieve the structure above would be most appreciated.

V / R

Chris

推荐答案

As其他提到的,你想要的结构是无效的。
我建议您的所有者文档使用以下结构:

As other mentioned, The structure you want is not valid. I recommend the following structure for your owner document:

    {
    "_id" : ObjectId("51c9cf2b206dfb73d666ae07"),
    "firstName" : "john",
    "lastName" : "smith",
    "ownerEmail" : "john.smith@gmail.com",
    "camps" : [
            {
                    "name" : "cubs-killeen",
                    "location" : "killeen"
            },
            {
                    "name" : "cubs-temple",
                    "location" : "temple"
            }
    ],
    "instructors" : [
            {
                    "firstName" : "joe",
                    "lastName" : "black"
            },
            {
                    "firstName" : "will",
                    "lastName" : "smith"
            }
    ]
}

然后

db.stack.update(
  { ownerEmail: "john.smith@gmail.com" },
  {
    $push: {
      camps: { name: "cubs-killeen", location: "some other Place" }
    }
  }
);

有了这个,你可以添加这样的阵营:

Having this, you can add camps like this:

希望有所帮助。

这篇关于MongoDb:如何将其他对象插入到对象集合中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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