You could try a different approach: create a unique index on both "name" and "group".
Unique indexes in the official documentation: https://docs.mongodb.com/manual/core/index-unique/
Run this in the command shell:
db.your_collection.createIndex( { "name": 1, "group": 1 }, { unique: true } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
When you'll try to insert another document with the same data you'll receive an error and the data won't be inserted:
> db.your_collection.insert({ name: "name1",group: "group1",source: "source1",context: "context1"})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: test.some index: name_1_group_1 dup key: { : \"name1\", : \"group1\" }"
}
})
For MongoDb > 2.4 tere is findAndModify: https://docs.mongodb.com/manual/reference/method/db.collection.findAndModify/