说我有一个数据结构是这样的:

{
    'name': 'test',
    'anotherdoc': {
        'something': 'someval',
        'somenum': 1
    }
}

现在,说我想设置的东西。起初,我以为它会像这样做:

collection.update({'_id': myid}, {$set: {'anotherdoc.something': 'somenewval'});

此,然而,似乎是不正确。它把一些数据在那里,但它在一个奇怪的方式这样做。这将在这种情况下,最终像这样:

[
    {
        'name': 'test',
        'anotherdoc': {
            'something': 'someval',
            'somenum': 1
        }
    },
    ['anotherdoc.something', 'someval']
]

当然,不是我所期待的。

有帮助吗?

解决方案

从蒙戈外壳为我下面的作品 - 所以我不知道上面发生了什么事给你。试试这个,看看是否可行?如果是的话,我会说抢在万一最新蒙戈代码中使用是有问题的。

x = { 'name': 'test', anotherdoc: { 'something': 'someval', somenum : 1 } }
> x
{"name" : "test" , "anotherdoc" : {"something" : "someval" , "somenum" : 1}}
> collection = db.foo;
test.foo
> collection.insert(x)
> collection.find()
{"_id" :  ObjectId( "4a61b6711591f41f0f1bc5ff")  , "name" : "test" , "anotherdoc" : {"something" : "someval" , "somenum" : 1}}
> x
{"name" : "test" , "anotherdoc" : {"something" : "someval" , "somenum" : 1}}
> x._id
> x = collection.findOne()
{"_id" :  ObjectId( "4a61b6711591f41f0f1bc5ff")  , "name" : "test" , "anotherdoc" : {"something" : "someval" , "somenum" : 1}}
> collection.update({'_id': x._id}, {$set: {'anotherdoc.something': 'somenewval'}} )
> collection.find()
{"_id" :  ObjectId( "4a61b6711591f41f0f1bc5ff")  , "name" : "test" , "anotherdoc" : {"somenum" : 1 , "something" : "somenewval"}}
> 

如上面所提到的,MongoDB的论坛可能会得到看出更快(或尝试IRC)。

其他提示

您最好在MongoDB中用户的googlegroup问这个。 你的问题的答案就在这里 http://groups.google的.com /组/ mongodb的用户/ MSG / 583d37ef41ef5cca?HL =烯

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top