遍历所有Mongo集合并执行查询

编程入门 行业动态 更新时间:2024-10-26 22:27:41
本文介绍了遍历所有Mongo集合并执行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

首先,我对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集合并执行查询

本文发布于:2023-11-22 20:18:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618865.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   Mongo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!