遍历所有Mongo集合并执行查询 [英] Loop through all Mongo collections and execute query
问题描述
首先,我对mongodb很新。这是我的问题,我无法找到解决方案。
First of, I'm quite new to mongodb. Here's my question I've not been able to find a solution to.
假设我有3个不同的收藏品。
Let's say I have 3 different collections.
mongos> show collections
collectionA
collectionB
collectionC
我想创建一个脚本,它遍历此数据库中的所有集合,并在每个集合中查找最后插入的时间戳。这是在mongos里面有用的东西。
I want to create a script that iterates over all collections ind this database and find the last inserted timestamp in each of these collections. Here's what works inside mongos.
var last_element = db.collectionA.find().sort({_id:-1}).limit(1);
printjson(last_element.next()._id.getTimestamp());
ISODate("2014-08-28T06:45:47Z")
1。问题(迭代所有收藏品)
是否有任何可能性......喜欢。
Is there any possibility to to sth. like.
var my_collections = show collections;
my_collections.forEach(function(current_collection){
print(current_collection);
});
问题在这里, my_collections
的分配确实如此不行。
我得到 SyntaxError:意外的标识符
。我需要引用'show'声明吗?它甚至可能吗?
Problem here, the assignment for my_collections
does not work.
I get SyntaxError: Unexpected identifier
. Do I need to quote the 'show' statement ? Is it even possible ?
2。问题(在js var中存储集合)
我可以通过这样做来解决问题1:
I can workaround Problem 1 by doing this:
var my_collections = ["collectionA", "collectionB", "collectionC"];
my_collections.forEach(function(current_collection){
var last_element = db.current_collection.find().sort({_id:-1}).limit(1);
print(current_collection);
printjson(last_element.next()._id.getTimestamp());
});
last_element.next()
产生以下错误:
错误hasNext:false在src / mongo / shell / query.js:124
error hasNext: false at src/mongo/shell/query.js:124
似乎last_element未正确保存。
It seems that last_element isn't saved correctly.
有关我做错的任何建议?
Any suggestions on what I'm doing wrong??
更新
Neils回答我的解决方案。除了他的代码,我还要检查函数 getTimestamp
是否真的存在。对于某些虚拟集合,似乎没有_id属性。
Neils answer lead me to this solution. In addition to his code I had to check if the function getTimestamp
really exist. For some 'virtual' collections there seem to be no _id property.
db.getCollectionNames().forEach(function(collname) {
var last_element = db[collname].find().sort({_id:-1}).limit(1);
if(last_element.hasNext()){
var next = last_element.next();
if(next._id !== undefined && typeof next._id.getTimestamp == 'function'){
printjson(collname + " >> "+next._id.getTimestamp());
}else{
print(collname + " undefined!! (getTimestamp N/A)")
}
}
});
推荐答案
有 db.getCollectionNames()
为您执行此操作的辅助方法。然后,您可以实现您的代码:
There is the db.getCollectionNames()
helper method that does this for you. You can then implement your code:
db.getCollectionNames().forEach(function(collname) {
// find the last item in a collection
var last_element = db[collname].find().sort({_id:-1}).limit(1);
// check that it's not empty
if (last_element.hasNext()) {
// print its timestamp
printjson(last_element.next()._id.getTimestamp());
}
})
您可能还需要 .hasNext ()
检查那里以迎合可能的空集合。
You probably also want a .hasNext()
check in there to cater for possible empty collections.
这篇关于遍历所有Mongo集合并执行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!