我的目标是以“page_length”降序的顺序将集合 1 中的每个项目复制到集合 2,这样新集合首先具有最大的 page_length 字段,第二是第二大字段,依此类推。
我在Mongo中尝试了以下内容:
db.mycollection1.find().sort({page_length:-1}).foreach(function(d) { db.mycollection2.insert({"field1":d.field1, "field2":d.field2}); })
得到一个错误,DBQuery 没有方法“foreach”。我知道 foreach() 是 find() 的一个函数,但我希望找到一种解决方法。我什至尝试了以下方法:
db.mycollection1.aggregate({$sort:{page_length:-1}}).foreach(function(d) { db.mycollection2.insert({"field1":d.field1, "field2":d.field2}); })
有任何想法吗?