0

If I have this document in a collection, how can I do a partial insert?

Collection

{
    "_id" : "123",
    "title" : "Green Tree",
    "detail" : [
        {
            "title" : "Just a title",
            "content" : "Content",
            "reference" : 456
        }
    ]
}

If I have a object with the same first level title, the just the detail should be added. So if I would add those two objects:

var newObject_1 = {
        "title" : "Green Tree",
        "detail" : [
            {
                "title" : "Just another title",
                "content" : "And another content",
                "reference" : 456
            }
        ]
    }
var newObject_2 = {
        "title" : "Red Car",
        "detail" : [
            {
                "title" : "title",
                "content" : "content",
                "reference" : 456
            }
        ]
    }
Collection.insert(newObject_1);
Collection.insert(newObject_2);

The result should be

{
    "_id" : "123",
    "title" : "Green Tree",
    "detail" : [
        {
            "title" : "Just a title",
            "content" : "Content",
            "reference" : 456
        },
        {
            "title" : "Just another title",
            "content" : "And another content",
            "reference" : 456
        }           
    ]
}
{
    "_id" : "124",
    "title" : "Red Car",
    "detail" : [
        {
            "title" : "title",
            "content" : "content",
            "reference" : 456
        }
    ]
}

If also the details have the same content, nothing should happen at all. If the title is different I just do Collection.insert(newObject); to add the data completly.

user3848987
  • 1,627
  • 1
  • 14
  • 31

1 Answers1

1

Maybe like an upsert?

MyCollection.upsert(
  { name: 'green-tree' },
  { 
    $setOnInsert: { details: [] },
    $set: { 'details': yourObj.details },
  }
)

Basically, it will try to find the document with the name provided. If it finds it, it will perform the options below. Otherwise, it will insert it and do the operations specified.

corvid
  • 10,733
  • 11
  • 61
  • 130
  • I'm not quite sure, if I understand this code: It looks if there is a name = 'green-tree'. If this is the case, the details of the object will be added to this doc. That's what `$push` does, right? So what is `setOnInsert` for? If I understand that correct, then it is not, what I need: First I want to check if name=green-tree is already in the collection. If not, just do a normal insert for the complete object to add this object to the collection. If name=green-tree already exist, then just the details are added to this name. If the details are matching, nothing should be done (=duplicate) – user3848987 Sep 17 '15 at 19:26
  • Are you looking for any document in the collection with `name: 'Green Tree'`? If so, you probably [want to use upsert](http://devdocs.io/meteor/index#upsert). It can either insert or update, depending on if the query (first param) yields any results. – corvid Sep 17 '15 at 19:41
  • Yes, I'm looking for any document in the collection with name: 'Green Tree'. If there is no result, the complete object should be inserted and this is what I do not see (or understand) in the code. In the code just the details are added. Am I wrong? – user3848987 Sep 17 '15 at 19:46