David Villasmil
David Villasmil

Reputation: 415

mongoDB documents with subdocuments

I have the following structure:

  • Collection
    • document
      • message
        • field 1
        • field 2
        • field 3

The document will start like before, I will need to find the document by searching like:

db.document.find( { "message.field1": "a" } )

and when found, add a another message as an array and will end up like this:

  • Collection
    • document
      • message
        • field 1
        • field 2
        • field 3
      • message (new doc)
        • field 1
        • field 2
        • field 3

I'm trying to do:

db.packets.update(
    { $or: [
        { "message.field1": "a" } , { "message.field2":  "a" }
    ]},
    {
        packet: {
            "packet" : {
                "field1" : "whatever",
                "field2" : "Whatever",
                "field3" : "blahblah"
            }
        }
    },
    {
        upsert: true
    } )

But mongo doesn't seem to find the subdocument I'm searching for... is it possible to have multiple arrays? I'm thinking maybe mongodb doesn't allow to have multiple subdocuments with the same name?

Thanks guys,

David

Upvotes: 0

Views: 110

Answers (1)

David Villasmil
David Villasmil

Reputation: 415

I found my problem, at least I think I did:

This:

{
    packet: {
        "packet" : {
            "field1" : "whatever",
            "field2" : "Whatever",
            "field3" : "blahblah"
        }
    }
}

Should actually be:

{
    "packet" : {
        "field1" : "whatever",
        "field2" : "Whatever",
        "field3" : "blahblah"
    }
}

With the first I was looking for "packets.packets.field1", and I'm using "packets.field1" so i had to change the hierarchy...

Thanks all for your help!

Upvotes: 0

Related Questions