Domanda

Dire che ho una struttura dati simile a questa:

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

Ora, dicono volevo impostare qualcosa. Inizialmente, ho pensato che sarebbe stato fatto in questo modo:

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

Questa, però, sembra non essere corretto. Lo fa mettere un po 'di dati in là, ma lo fa in un modo strano. Sarebbe, in questo caso, finiscono in questo modo:

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

Naturalmente, non quello che stavo cercando.

È stato utile?

Soluzione

I seguenti lavori per me dalla shell mongo - quindi non sono sicuro di quello che è accaduto in precedenza per voi. Provate questo e vedere se funziona? Se quindi direi afferrare l'ultimo codice mongo nel caso in cui qualcosa di usato per essere problematico.

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

Come accennato in precedenza, i forum MongoDB probabilmente ottenere visti più veloce (o provare IRC).

Altri suggerimenti

È meglio chiedere questo googlegroup dell'utente MongoDB. La risposta per la tua domanda è qui http://groups.google .com / gruppo / mongodb-user / msg / 583d37ef41ef5cca? hl = it

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top