title: mongodb setdifference between collection
body: <p>I have the following 2 collection in mongodb</p>
  
  <pre><code>db.test.insert({"Name":"a","keyid":1,"description":"test"})
  db.test.insert({"Name":"a","keyid":2,"description":"test"})
  db.test.insert({"Name":"a","keyid":3,"description":"test"})
  db.test.insert({"Name":"a","keyid":5,"description":"test"})
  db.test.insert({"Name":"a","keyid":6,"description":"test"})
  db.test.insert({"Name":"b","keyid":3,"description":"test"})
  db.test.insert({"Name":"b","keyid":4,"description":"test"})
  
  
  db.test1.insert({"Name":"a","keyid":1,"description":"test"})
  db.test1.insert({"Name":"a","keyid":2,"description":"test"})
  db.test1.insert({"Name":"a","keyid":8,"description":"test"})
  db.test1.insert({"Name":"a","keyid":5,"description":"test"})
  db.test1.insert({"Name":"a","keyid":6,"description":"test"})
  </code></pre>
  
  <p>I would like to insert documents from test to test1 for key id and name that are not present in test1 which in this case are </p>
  
  <pre><code>db.test1.insert({"Name":"a","keyid":3,"description":"test"})
  db.test1.insert({"Name":"b","keyid":3,"description":"test"})
  db.test1.insert({"Name":"b","keyid":4,"description":"test"})
  </code></pre>
  
  <p>and similary i would like to insert documents from test1 to test for key id and name that are not present in test which in this case is </p>
  
  <pre><code> db.test.insert({"Name":"a","keyid":8,"description":"test"})
  </code></pre>
  
  <p>Basically i am try to perform setdifference between the 2 collection </p>
  
  <p>Currently i am doing </p>
  
  <pre><code>db.test.aggregate([
     {"$group" : {_id : {Name:"$Name", keyid:"$keyid"}}}, 
     {"$project" : { Name:"$_id.Name",keyid:"$_id.keyid"}}
  ], { allowDiskUse: true })
  </code></pre>
  
  <p>And</p>
  
  <pre><code>db.test1.aggregate([
     {"$group" : {_id : {Name:"$Name", keyid:"$keyid"}}}, 
     {"$project" : { Name:"$_id.Name",keyid:"$_id.keyid"}}
  ], { allowDiskUse: true })
  </code></pre>
  
  <p>Then performing whether the output in first aggregation pipeline exist in the output of second aggregation pipeline and vice versa .</p>
  
  <p>Is there a better way of doing this using the set operation in mongodb.</p>