我有一个有2条记录的集合aTable:
{
"title" : "record 1",
"fields" : [
{
"_id" : 1,
"items" : [
1
]
},
{
"_id" : 2,
"items" : [
2,3,4
]
},
{
"_id" : 3,
"items" : [
5
]
}
]
},
{
"title" : "record 2",
"fields" : [
{
"_id" : 4,
"items" : [
7,8,9,10
]
},
{
"_id" : 5,
"items" : [
]
},
{
"_id" : 6,
"items" : [
11,12
]
} …Run Code Online (Sandbox Code Playgroud) 在shell中,我的查询是:
db.checkin_4e95ae0926abe9ad28000001.update({location_city:"New York"}, {location_country: "FUDGE!"});
Run Code Online (Sandbox Code Playgroud)
但是,它实际上并没有更新我的记录.它也没有错误.当我执行db.checkin_4e95ae0926abe9ad28000001.find({location_city:"New York"});此操作后,我得到了所有结果,但location_country没有改变:
{
"_id": ObjectId("4e970209a0290b70660009e9"),
"addedOn": ISODate("2011-10-13T15:21:45.772Z"),
"location_address1": "",
"location_city": "New York",
"location_country": "United States",
"location_latLong": {
"xLon": -74.007124,
"yLat": 40.71455
},
"location_source": "socialprofile",
"location_state": "New York",
"location_zip": ""
}
Run Code Online (Sandbox Code Playgroud) 我只是玩mongo shell而且遇到了Cannot use commands write mode, degrading to compatibility mode.
我连接到远程mongo服务器(mongolab)并尝试通过我的简单脚本将新记录插入到集合中:
// script.js
db = connect(host + ":" + port +"/" + dbName);
db.auth(username, password);
db.test2.insert({ item: "card", qty: 15 });
Run Code Online (Sandbox Code Playgroud)
我运行脚本mongo script.js并得到:
MongoDB shell version: 2.6.3
connecting to: test
connecting to: my.mongolab.com:port/DBname
Cannot use commands write mode, degrading to compatibility mode
Run Code Online (Sandbox Code Playgroud)
怎么了?另外当我连接后执行类似的查询mongo my.mongolab.com:port/DBname -u <dbuser> -p <dbpassword>一切都OK.
首先,我对mongodb很新.这是我的问题,我无法找到解决方案.
假设我有3个不同的收藏品.
mongos> show collections
collectionA
collectionB
collectionC
Run Code Online (Sandbox Code Playgroud)
我想创建一个脚本,遍历此数据库中的所有集合,并在每个集合中查找最后插入的时间戳.这是在mongos里面有效的方法.
var last_element = db.collectionA.find().sort({_id:-1}).limit(1);
printjson(last_element.next()._id.getTimestamp());
ISODate("2014-08-28T06:45:47Z")
Run Code Online (Sandbox Code Playgroud)
1.问题(迭代所有集合)
是否有任何可能性...... 喜欢.
var my_collections = show collections;
my_collections.forEach(function(current_collection){
print(current_collection);
});
Run Code Online (Sandbox Code Playgroud)
问题在这里,分配my_collections不起作用.我得到SyntaxError: Unexpected identifier.我需要引用'show'声明吗?它甚至可能吗?
2.问题(在js var中存储集合)
我可以通过这样做来解决问题1:
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());
});
Run Code Online (Sandbox Code Playgroud)
将last_element.next()产生以下错误:
错误hasNext:在src/mongo/shell/query.js中为false:124
似乎last_element未正确保存.
关于我做错什么的任何建议?
UPDATE
尼尔斯回答了我的解决方案.除了他的代码,我还要检查函数是否getTimestamp真的存在.对于某些"虚拟"集合,似乎没有_id属性.
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 …Run Code Online (Sandbox Code Playgroud) 我只想针对特定的mongodb数据库运行.js脚本,但似乎无法获得正确的语法.
echo "print(db.address.find().limit(1));" > test.js
mongo test.js
Run Code Online (Sandbox Code Playgroud)
如何选择将要执行的数据库,我尝试了各种组合,但use foo没有成功.
> show dbs
admin (empty)
local (empty)
foo 0.203125GB
bar 0.203125GB
Run Code Online (Sandbox Code Playgroud)
我想foo在这种情况下使用.
我将文档插入到mongoDB中的特定数据库中.例如
db.employee.insert({name:"nithin",age:22})
db.employee.insert({name:"sreedevi",age:32})
Run Code Online (Sandbox Code Playgroud)
现在我想要检索名字以' i ' 结尾的文件.
我是MongoDB的新手,我无法找到我正在寻找的解决方案.
我想迭代所有mongo数据库并在每个数据库的每个集合上运行一些命令.我可以运行以下命令来获取所有数据库名称:
db.runCommand( { listDatabases: 1 } ).databases.forEach(function (db) {
print ("db=" + db.name);
});
Run Code Online (Sandbox Code Playgroud)
但是如何在forEach循环中"切换"数据库以便我可以针对每个数据库运行查询?我想使用像use db.name循环内部的东西,但这不起作用.
我有一个非常复杂的问题,我认为我可以通过编写一个mongo shell脚本来解决,但我甚至无法建立一个简单的连接.我有一个本地mongo数据库,需要一个我通常访问的用户名/密码,如下所示:
mongo admin -u <username> -p
Run Code Online (Sandbox Code Playgroud)
在这一点上,我输入密码和万岁!我有一个壳.但这对我的问题不起作用.作为测试,我创建了一个名为test.js的文件,其中包含的是:
var conn = new Mongo()
db = conn.getDB("test");
db.cust.find();
Run Code Online (Sandbox Code Playgroud)
然后我从命令行运行脚本,如下所示:
mongo test.js
Run Code Online (Sandbox Code Playgroud)
在这一点上,我得到这个:
MongoDB shell version: 2.4.10
connecting to: test
Run Code Online (Sandbox Code Playgroud)
为什么我没有结果?
如何在 MongoDB shell 中复制和粘贴文本?
我尝试了Ctrl+C和Ctrl+V但它没有用。
谢谢,迈克尔。
我尝试从 atlas 恢复 mongo 备份文件。
它包含一些 wt 文件。如何恢复。
从 atlas 的每日快照下载备份。
提前致谢。