简体   繁体   English

连接到mongo shell中的副本集

[英]Connect to replica set within mongo shell

I'm trying to write a Mongo shell script that connects to two databases, searches for some document(s) in one, and inserts the found document(s) into the other. 我正在尝试编写一个Mongo Shell脚本,该脚本连接到两个数据库,在一个数据库中搜索一些文档,然后将找到的文档插入另一个数据库。 Kinda like this: 有点像这样:

#!/bin/sh
mongo --shell --nodb <<EOF
var db1 = new Mongo( '...' );
var db2 = new Mongo( '...' );
db1.collection.findOne( {...} ).forEach( function( r ) { 
  db2.collection.save( r ) 
});

The trick is, both databases are replica sets, and require a username and password. 诀窍是,两个数据库都是副本集,并且需要用户名和密码。

What is the syntax for using new Mongo() to connect to a replica set and authenticating as a particular user? 使用new Mongo()连接到副本集并以特定用户身份验证的语法是什么? I tried to use a Mongo URI ( http://docs.mongodb.org/manual/reference/connection-string/ ) but that didn't work. 我尝试使用Mongo URI( http://docs.mongodb.org/manual/reference/connection-string/ ),但这没有用。

I don't have a replica set to test this on but I think you can use the Mongo() constructor like this 我没有设置副本来测试,但我认为您可以使用像这样的Mongo()构造函数

conn = Mongo("replicasetname/host:port")

from there I think you'll need to get the database manually with 从那里,我认为您需要手动获取数据库

db = conn.getDB("myDatabase")

and then authenticate 然后验证

db.auth(user, pass)

This could all depend on what shell version you're using as well. 这都可能取决于您所使用的Shell版本。 I don't see any documentation on using the replica set connection in the latest version so I don't know if it's deprecated or just not mentioned anymore. 我没有看到任何有关在最新版本中使用副本集连接的文档,所以我不知道它是否已被弃用或不再提及。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM