문제

다음과 같은 데이터 구조가 있다고 가정 해보십시오.

{
    '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/group/mongodb-user/msg/583d37ef41ef5cca?hl=en

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top