我有一个具有以下结构的mongodb文档
> db.user.find().limit(1);
{ "_id" : "1", "foo" : { "bars" : [
{
"name" : "bar1"
},
{
"name" : "bar2"
},
], ... }, ... }
Run Code Online (Sandbox Code Playgroud)
我想为每个人添加一个新属性bar.我已经让我的脚本迭代了bars数组,但我无法在那里获得新属性,我该怎么做?
var users = db.user.find({"foo.bars":{$exists:true}});
users.forEach(function(user) {
user.foo.bars.forEach(function(bar)
{
printjson(bar);
//how can I specify the current 'bar' in this update?
//db.experience.update({_id: user._id}, {$set: {"what goes here?" : "newbarValue"}});
});
});
Run Code Online (Sandbox Code Playgroud) 这是我正在尝试做的一个例子,因为我无法解释我的意思.
我在mongo CLI中执行此操作:db.posts.insert({name: "Hello, world!, body: "Here's my first blog post. Happy reading!", comments: []}).我想要做的是更新该条目以向comments词典添加注释.
我将如何实现这一目标db.posts.update()?
基本上问题很简单:
如何对以 _ 开头的集合发出查询?
例如,如果我有 2 个集合testand _test,并且我正在尝试db.test.findOne()并且db._test.findOne()在mongoshell第一个集合中按预期工作,而第二个告诉我TypeError: db._testhas no properties (shell):1
发生过几次我在集合名称中打错字或忘记将数据库切换到包含所需集合的数据库。Mongo shell 默默地接受了所有这些命令。如果我尝试对不存在的集合执行查询,mongo shell 是否可能会警告我?
我有一个集合 TextDocuments
/* 0 */
{
name:"doc2",
pages:[{
pageNumber:"1",
text:"This is first page text",
},{
pageNumber:"2",
text:"This is second page text",
},{
pageNumber:"3",
text:"This is third page text",
}]
}
/* 1 */
{
name:"doc2",
pages:[{
pageNumber:"1",
text:"This is first page text",
},{
pageNumber:"2",
text:"This is second page text",
},{
pageNumber:"3",
text:"This is third page text",
}]
}
Run Code Online (Sandbox Code Playgroud)
当我在 mongo shell 中运行以下查询时,我想从具有 name = doc2 的集合 TextDocuments 中删除文档
rohitkumar@ubuntuhost:~$ mongo
> use mydb
switched to db mydb
> db.TextDocuments.remove({"name":"doc2"})
WriteResult({ …Run Code Online (Sandbox Code Playgroud) 我正在使用 MongoDB shell 并且喜欢定义一些快捷方式。例如,缩写show databases为sd.
我已经设法hw()通过将它的定义添加到 MongoDB shell 中添加了一个函数~/.mongorc.js:
function hw() {
print("Hello World.");
}
Run Code Online (Sandbox Code Playgroud)
当我输入hw()mongo-shell 时,它会打印出来Hello World.
问题 1:是否也可以执行函数而不必键入括号(即hw代替hw())?
我尝试使用匿名函数将函数绑定到变量,但我仍然必须输入括号,否则会打印出函数的定义
hw=function(){ print("Hello World (anonymous)."); };
Run Code Online (Sandbox Code Playgroud)
问题 2:如何从我的函数中执行 MongoDB 命令?我试过:
function sd() {
show databases;
}
Run Code Online (Sandbox Code Playgroud)
但这会在 MongoDB shell 启动期间出现错误:
语法错误:/home/edward/.mongorc.js:2 处的意外标识符
我能够从 intellij 连接到 mongo db。Mongo 资源管理器工作正常。
但是连接后,当我尝试在任何数据库上打开 Mongo Shell 时,它无法连接。
Intellij 上出现错误 我在 Windows 7 上运行时抛出以下错误
2015-10-12T15:08:24.062-0700 I CONTROL Hotfix KB2731284 or later update is installed, no need to zero-out data files
Invalid command: localhost:27017/students
Options:
General options:
-h [ --help ] show this usage information
--version show version information
-f [ --config ] arg configuration file specifying additional options
-v [ --verbose ] [=arg(=v)] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--quiet quieter output
--port …Run Code Online (Sandbox Code Playgroud) 我在一些集合上将一些简单的游标定义为find().
cursor = db.students.find()
Run Code Online (Sandbox Code Playgroud)
学生有以下架构:
"_id" : 19,
"name" : "Gisela Levin",
"scores" : [
{
"type" : "exam",
"score" : 44.51211101958831
},
{
"type" : "quiz",
"score" : 0.6578497966368002
},
{
"type" : "homework",
"score" : 93.36341655949683
},
{
"type" : "homework",
"score" : 49.43132782777443
}
]
Run Code Online (Sandbox Code Playgroud)
我想迭代光标并打印文档.我试着这样:
cursor.forEach(function(o){print(o)})
Run Code Online (Sandbox Code Playgroud)
它只打印:
[对象]
如果我修改一个循环来选择一些简单的属性,它可以工作:
cursor.forEach(function(o){print(o._id)})
Run Code Online (Sandbox Code Playgroud)
虽然如果我把它更改为打印分数,它会打印出来:
[object Object],[object Object],[object Object],[object Object]
Run Code Online (Sandbox Code Playgroud)
是否有任何函数在mongodb shell中打印json文档?
我认为这是一个非常基本的问题,但我被困住了.我通过MongoDB shell连接到远程mongo实例(mLab).对于单行来说这很好,但是现在我想要运行更大的命令,但是经常需要从已经连接的shell中执行它.
如何从mongo shell运行我的本地script.js并获取shell中的输出,就好像我只是按常规运行单行程?
我希望load("script.js")会这样做,但无论内容如何,它都会返回'true'.
这是我要执行的 JavaScript 函数:
var collectionCreation = function(){
db.myFirstCollection.find();
};
collectionCreation();
Run Code Online (Sandbox Code Playgroud)
从我的命令提示符指向 bin 目录,我想执行包含上述代码的 js 文件。我想这样做:
mongo localhost:27017/myFirstDatabase G:\MongoDB\createColl.js
Run Code Online (Sandbox Code Playgroud)
它没有显示任何输出。我期待在我的收藏中得到这些文件。请帮忙。提前致谢。