案例如下:
集合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数据库-四书五经-孟子,导入即可使用 MongoDB数据库-四书五经-孟子,导入即可使用 MongoDB数据库-四书五经-孟子,导入即可使用 MongoDB数据库-四书五经-孟子,导入即可使用 MongoDB数据库-四书五经-孟子,...
MongoDB 数据库-四书五经-中庸,导入直接使用 MongoDB 数据库-四书五经-中庸,导入直接使用 MongoDB 数据库-四书五经-中庸,导入直接使用 MongoDB 数据库-四书五经-中庸,导入直接使用 MongoDB 数据库-四书五经-中庸...
MongoDB数据库文件-名言警句集合,导入即可使用 MongoDB数据库文件-名言警句集合,导入即可使用 MongoDB数据库文件-名言警句集合,导入即可使用 MongoDB数据库文件-名言警句集合,导入即可使用 MongoDB数据库文件-...
爬虫开发阶段-爬虫基础-MongoDB数据库-爬虫Scrapy框架和案例.内有丰富的开发案例,希望对大家有用
MongoDB数据库的安装包,mongodb-win32-x86_64-2008plus-ssl-4.0.9-signed.msi
网易云课堂-『李兴华java培训23』MongoDB数据库-笔记 最后2节因为是JAVA开发环境,因为没有使用JAVA开发,所有使用的是截图。 基础课,没有哪么多的废话,听听还不错。
MongoDB--全面MongoDB--全面MongoDB--全面MongoDB--全面MongoDB--全面MongoDB--全面MongoDB--全面MongoDB--全面MongoDB--全面MongoDB--全面MongoDB--全面MongoDB--全面MongoDB--全面MongoDB--全面MongoDB--全面...
mongoDB-CURD操作-----JAVA 对初学入门还是有点用处滴!
mongodb-windows安装包: mongodb-compass-1.31.2-win32-x64.msi 打开直接安装
MongoDB Community Server(mongodb-linux-aarch64-ubuntu1804-5.0.8.tgz)适用于Ubuntu 18.04 Arm芯片, MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决...
MongoDB Community Server(mongodb-org-shell-5.0.4-1.el7.x86_64.rpm)适用于RedHat / CentOS 7.0 MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 ...
MongoDB Community Server(mongodb-org-server-5.0.4-1.el7.x86_64.rpm)适用于RedHat / CentOS 7.0 MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。...
MongoDB Community Server(mongodb-windows-x86_64-5.0.8-signed.msi)适用于RedHat / CentOS 7.0 MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 ...
MongoDB Community Server(mongodb-src-r5.0.4.tar.gz)源代码 MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB是一个介于关系数据库和非...
MongoDB Community Server(mongodb-org-server_5.0.4_amd64.deb)适用于适用于Debian10 MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB是...
MongoDB Community Server(mongodb-org-server-5.0.8-1.el7.x86_64.rpm)适用于RedHat / CentOS 7.0 MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。...
java和mongodb连接,需要mongodb-driver,您还必须下载其依赖项: bson和 mongodb-driver-core》》3个包: mongodb-driver-3.8.2.jar; bson-3.8.2.jar; mongodb-driver-core-3.8.2.jar
MongoDB window 安装包,mongodb-windows-x86_64-4.4.0-signed.msi,免除下载速度慢的烦恼!
由于mongoDB官网的下载速度实在是太慢了,所以我把社区版本下下来了,传到CSDN供大家下载,不然下载一个数据库得一个小时
mongodb-windows-x86_64-7.0.5-signed.msi 数据库构建工具