mongodb数据库---本地集合拷贝克隆、筛选剔除

 

案例如下:

集合A,B,分别有a,b,c,d,e属性,要求从集合A中根据关联属性剔除掉B集合中的数据,得到新的数据集C,C的展示属性为a,b,其中关联属性为a,b,c。

即:遍历集合A中每条数据,判定集合B中不存在与集合A中属性a,b,c相等的数据则符合要求并保存在集合C中。

 

要求:同一个应用服务下,同一用户

 

具体操作如下:

 

> db.A.find()
{ "_id" : ObjectId("5018da521781352fe25bf4d2"), "a" : "1", "b" : "1", "c" : "1", "d" : "1", "e" : "1" }
{ "_id" : ObjectId("5018da5a1781352fe25bf4d3"), "a" : "2", "b" : "2", "c" : "2", "d" : "2", "e" : "2" }
{ "_id" : ObjectId("5018da821781352fe25bf4d4"), "a" : "3", "b" : "3", "c" : "3", "d" : "3", "e" : "3" }
{ "_id" : ObjectId("5018da891781352fe25bf4d5"), "a" : "4", "b" : "4", "c" : "4", "d" : "4", "e" : "4" }
{ "_id" : ObjectId("5018da931781352fe25bf4d6"), "a" : "5", "b" : "5", "c" : "5", "d" : "5", "e" : "5" }
{ "_id" : ObjectId("5018da9d1781352fe25bf4d7"), "a" : "6", "b" : "6", "c" : "6", "d" : "6", "e" : "6" }
{ "_id" : ObjectId("5018daa71781352fe25bf4d8"), "a" : "7", "b" : "7", "c" : "7", "d" : "7", "e" : "7" }
{ "_id" : ObjectId("5018daa71781352fe25bf4d9"), "a" : "8", "b" : "8", "c" : "8", "d" : "8", "e" : "8" }
{ "_id" : ObjectId("5018daa71781352fe25bf4da"), "a" : "9", "b" : "9", "c" : "9", "d" : "9", "e" : "9" }
{ "_id" : ObjectId("5018daa71781352fe25bf4db"), "a" : "10", "b" : "10", "c" : "10", "d" : "10", "e" : "10" }
{ "_id" : ObjectId("5018daa71781352fe25bf4dc"), "a" : "11", "b" : "11", "c" : "11", "d" : "11", "e" : "11" }
{ "_id" : ObjectId("5018daa71781352fe25bf4dd"), "a" : "12", "b" : "12", "c" : "12", "d" : "12", "e" : "12" }
{ "_id" : ObjectId("5018daa71781352fe25bf4de"), "a" : "13", "b" : "13", "c" : "13", "d" : "13", "e" : "13" }
{ "_id" : ObjectId("5018daa71781352fe25bf4df"), "a" : "14", "b" : "14", "c" : "14", "d" : "14", "e" : "14" }
{ "_id" : ObjectId("5018daae1781352fe25bf4e0"), "a" : "15", "b" : "15", "c" : "15", "d" : "15", "e" : "15" }

 

> db.B.find()
{ "_id" : ObjectId("5018dac11781352fe25bf4e1"), "a" : "1", "b" : "1", "c" : "1"}
{ "_id" : ObjectId("5018dac11781352fe25bf4e2"), "a" : "2", "b" : "2", "c" : "2"}
{ "_id" : ObjectId("5018dac11781352fe25bf4e3"), "a" : "3", "b" : "3", "c" : "3"}
{ "_id" : ObjectId("5018dac11781352fe25bf4e4"), "a" : "4", "b" : "4", "c" : "4"}
{ "_id" : ObjectId("5018dac11781352fe25bf4e5"), "a" : "5", "b" : "5", "c" : "5"}
{ "_id" : ObjectId("5018dac11781352fe25bf4e6"), "a" : "6", "b" : "6", "c" : "6"}
{ "_id" : ObjectId("5018dac11781352fe25bf4e7"), "a" : "7", "b" : "7", "c" : "7"}
{ "_id" : ObjectId("5018dac11781352fe25bf4e8"), "a" : "8", "b" : "8", "c" : "8"}
{ "_id" : ObjectId("5018dac11781352fe25bf4e9"), "a" : "9", "b" : "9", "c" : "9"}
{ "_id" : ObjectId("5018dac11781352fe25bf4ea"), "a" : "20", "b" : "20", "c" : "20" }
{ "_id" : ObjectId("5018dac31781352fe25bf4eb"), "a" : "21", "b" : "21", "c" : "21" }

 

> db.A.find().forEach(
... function(x){
...   var obj1 = db.B.findOne({"a":x.a,"b":x.b,"c":x.c});
...   if(obj1 == undefined || obj1 == null)
...   {
...   var obj2 = {"a":x.a,"b":x.b}
...   db.C.insert(obj2);
...   }
... }
... )

 

> db.C.find()
{ "_id" : ObjectId("5018fccd1781352fe25bf50d"), "a" : "10", "b" : "10" }
{ "_id" : ObjectId("5018fccd1781352fe25bf50e"), "a" : "11", "b" : "11" }
{ "_id" : ObjectId("5018fccd1781352fe25bf50f"), "a" : "12", "b" : "12" }
{ "_id" : ObjectId("5018fccd1781352fe25bf510"), "a" : "13", "b" : "13" }
{ "_id" : ObjectId("5018fccd1781352fe25bf511"), "a" : "14", "b" : "14" }
{ "_id" : ObjectId("5018fccd1781352fe25bf512"), "a" : "15", "b" : "15" }

 

实际处理脚本,从集合A中查找文档并循环遍历:

> db.A.find().forEach(
 function(x){
   --以集合A中的每条文档的a,b,c属性做条件查找集合B
   var obj1 = db.B.findOne({"a":x.a,"b":x.b,"c":x.c});
   --若找不到,即在集合B中不存在,则满足要求
   if(obj1 == undefined || obj1 == null)
   {
    --创建新的文档并保存在集合C中
    var obj2 = {"a":x.a,"b":x.b}
    db.C.insert(obj2);
   }
 }
)

 

 

 

 

你可能感兴趣的:(mongodb本地集合拷贝)