0

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

David Villasmil
  • 395
  • 2
  • 19
  • Are the messages in an array? And your updating doesn't make any sense. There's no operator. – ma08 Aug 03 '14 at 18:23
  • Could you share one document as it is stored in the db -the result of ```db.packets.findOne()```- and how do you want it to be once the update has been done ? – Enrique Fueyo Aug 03 '14 at 18:46

1 Answers1

0

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!

David Villasmil
  • 395
  • 2
  • 19