Pergunta

dizer que tenho uma estrutura de dados algo como isto:

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

Agora, digamos que eu queria definir alguma coisa. Inicialmente, eu pensei que seria feito da seguinte forma:

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

Isso, no entanto, parece ser incorreta. Ele faz colocar alguns dados lá, mas fá-lo de uma forma estranha. Seria, neste caso, acabar assim:

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

É claro, não o que eu estava procurando.

Foi útil?

Solução

As seguintes obras para me do shell mongo - então eu não tenho certeza do que aconteceu anteriormente para você. Experimente e veja se ele funciona? Se assim for, eu diria agarrar o código mais recente mongo no caso de algo costumava ser problemático.

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"}}
> 

Como mencionado acima, os fóruns MongoDB, provavelmente, obter visto mais rápido (ou tente IRC).

Outras dicas

É melhor você perguntar isso em googlegroup do usuário MongoDB. A resposta para sua pergunta é aqui http://groups.google .com / grupo / mongodb-user / msg / 583d37ef41ef5cca? hl = en

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top