我有2个收藏:
列表 1 和列表 2。
list1 有一些字段,list2 有其他字段,包括引用 list1 的 id。
我需要执行一个查询来导出 list1 上的所有项目,这些项目在 list2 上至少有一个项目引用了他。
我怎样才能做到这一点?这类似于从 list1 到 list2 的联接。
我需要运行 mongoexport 命令来生成 csv 文件。
我这样做的方法是创建一个简短的 javascript 程序,它将您想要导出的数据传输到一个新的临时集合中,然后您可以导出该集合。
例如,创建文件export.js:
//initialise the export results collection
db.export.results.drop();
//create a cursor containing the contents of the list1 collection
cursor = db.list1.find();
while (cursor.hasNext()) {
doc = cursor.next();
//Check if the document exists in the list2 collection
list2 = db.list2.find({"<id_fieldname>": doc.<id_fieldname>});
if (list2.hasNext()) {
//if it does exist, add the document from list1 to the new export collection
db.export.results.insert(doc);
}
}
print(db.export.results.count() + " matching documents found");
然后你可以从命令行运行它:
# mongo "localhost:27017/<dbname>" export.js
这将创建一个名为export.results的集合,其中包含list1集合中的文档以及list2集合中具有匹配id字段的文档。然后您可以导出或转储该集合:
# mongoexport --db <dbname> -c export.results -type csv -o <file_name>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)