我有mongodb atlas的副本集,我可以使用任何其他语言连接到该副本集,以及常规的mongo客户端,其中url提供以下格式:
"mongodb的://用户:pass@prefix1.mongodb.net:27017,prefix2.mongodb.net:27017,prefix3.mongodb.net:27017 /试验&replicaSet = Cluster0-碎片-0&authSource =管理员?"
无论我尝试什么,添加ssl = true和删除,没有任何作用.它始终是"无法访问的服务器".
我尝试了每个组合的url,dialConfig的每个组合,以及Dial和DialWithConfig配置.
可能是什么原因 ?
我检查了其他类似的帖子,但没有一个适合我的情况。
在连接到MongoDB Atlas之前,我升级了mongo shell,从中创建了一个免费的沙箱数据库。我将以下内容用于我的联系,这是由地图集提供的。
mongo "mongodb+srv://cluster0-z2lf6.mongodb.net/test" --authenticationDatabase admin --username <user> --password <password>
Run Code Online (Sandbox Code Playgroud)
数据库用户的图像:

数据库概述的屏幕快照:

这些是数据库的详细信息:

但是,终端显示此内容(我只包含了大约15行,其余为重复。):
MongoDB shell version v3.6.0
connecting to: mongodb+srv://cluster0-z2lf6.mongodb.net/test
2017-12-24T14:39:42.806+0800 I NETWORK [thread1] Starting new replica set monitor for Cluster0-shard-0/cluster0-shard-00-00-z2lf6.mongodb.net.:27017,cluster0-shard-00-01-z2lf6.mongodb.net.:27017,cluster0-shard-00-02-z2lf6.mongodb.net.:27017
2017-12-24T14:39:42.990+0800 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] Failed to connect to 34.232.245.97:27017, in(checking socket for error after poll), reason: Connection refused
2017-12-24T14:39:42.991+0800 W NETWORK [thread1] Failed to connect to 35.168.27.238:27017, in(checking socket for error after poll), reason: Connection refused
2017-12-24T14:39:43.492+0800 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] Failed to connect to 52.20.90.64:27017, in(checking …Run Code Online (Sandbox Code Playgroud) 我是go和mongodb的初学者。我尝试DocumentResult使用bson标签将a 解码为结构,但不适用于包装字符串的自定义类型。可以在不将字段类型更改为字符串的情况下完成此操作吗?
import (
"context"
"github.com/mongodb/mongo-go-driver/mongo"
)
type MyDoc struct {
SomeInt int `bson:"some_int"`
SomeString string `bson:"some_string,omitempty"`
CustomType MyType `bson:"custom_type,omitempty"`
}
type MyType string
const myType MyType = "ABCD"
func main() {
//Connect to db
client, _ := mongo.Connect(context.Background(), "mongodb://localhost:27017", nil)
db := client.Database("example_db")
collection := db.Collection("col")
//Insert document
docToInsert := MyDoc{42, "The Answer", myType}
collection.InsertOne(nil, docToInsert)
//Retrieve document
filterDoc := MyDoc{SomeInt: 42}
resultDoc := &MyDoc{}
result := collection.FindOne(nil, filterDoc)
result.Decode(resultDoc)
println(resultDoc.SomeInt, resultDoc.SomeString, resultDoc.CustomType)
Run Code Online (Sandbox Code Playgroud)
印刷结果:“ 42 The Answer” …
我正在尝试查询 mongo db 中的二进制字段。数据如下所示:
{"_id":"WE8fSixi8EuWnUiThhZdlw=="}
Run Code Online (Sandbox Code Playgroud)
我尝试了很多东西,例如:
{ '_id': new Binary( 'WE8fSixi8EuWnUiThhZdlw==', Binary.SUBTYPE_DEFAULT) }
{ '_id': Binary( 'WE8fSixi8EuWnUiThhZdlw==', 0) }
Run Code Online (Sandbox Code Playgroud)
等等
似乎没有任何工作,已经用尽了 google 和 mongo 文档,任何帮助都会很棒。
例如一个树结构;
[
{id: 1 , childrenIdList: [2, 3]},
{id: 2 , childrenIdList: [4, 5]},
{id: 3 , childrenIdList: []},
{id: 4 , childrenIdList: [6, 7]},
{id: 5 , childrenIdList: []},
{id: 6 , childrenIdList: []},
{id: 7 , childrenIdList: []}
]
Run Code Online (Sandbox Code Playgroud)
就像
1
2 3
4 5
6 7
Run Code Online (Sandbox Code Playgroud)
如何从启动叶节点(id = 7)到根(id = 1)跟踪树?
寻找的父母id=7很容易;因为
db.document.find({childrenList: { $in: [7]}}, {id: 1}).toArray(function(err), result{
/*result gives
{"id" : NumberInt(4)}
now I should look the parent of id=4, and parent of …Run Code Online (Sandbox Code Playgroud) 我可以在mongoDB Stitch 文档中看到limit选项,但是找不到如何跳过分页记录的方法。
我有 python 2.7,有 pymongo 3.4
我正在尝试使用以下方法连接到 mongo atlas:
import certifi
import ssl
import os
from pymongo import MongoClient
MongoClient(
"mongodb://Admin:<PASSWORD>@ABC-shard-00-00-XYZ:27017",
"ABC-shard-00-01-XYZ:27017",
"ABC-shard-00-02-XYZ:27017/dev?ssl=true&replicaSet=ABC-0&authSource=admin",
ssl_cert_reqs=ssl.CERT_REQUIRED,
ssl_ca_certs=certifi.where()
)
Run Code Online (Sandbox Code Playgroud)
它不断失败,身份验证失败。代码 18
我不知道如何连接到使用mongoengine的身份验证数据库的mongodb数据库.
在我需要的命令提示符上mongo hostname:27017/myApp -u "test" -p "test" --authenticationDatabase admin,但是我没有看到我将其作为mongoengine的参数传递给我,所以我使用admin数据库进行身份验证但是为我的模型连接到myApp数据库?
我相信这是在PyMongo指南中解释的地方:
https://api.mongodb.com/python/current/examples/authentication.html
>>> from pymongo import MongoClient
>>> client = MongoClient('example.com')
>>> db = client.the_database
>>> db.authenticate('user', 'password', source='source_database')
Run Code Online (Sandbox Code Playgroud)
我找到了将此添加到mongoengine的pull请求:
https://github.com/MongoEngine/mongoengine/pull/590/files
看起来你只是authentication_source作为一个参数添加connect喜欢connect(authentication_source='admin').如果记录得更好,那就太好了.
http://docs.mongoengine.org/apireference.html?highlight=authentication_source
我有一个带有日期字段的集合:
{
"_id" : ObjectId("5b92b359ddceef5b24502834"),
"dateTimeGMT" : ISODate("2018-08-22T09:29:25.000Z"),
yada, yada, yada
}
Run Code Online (Sandbox Code Playgroud)
我试图使用 mongo-go-driver 的 ParseExtJSONArray 函数在 $match 聚合阶段按日期查找。(我知道如何直接使用 *bson.Array 执行此操作。我问是为了知道使用 ParserExtJSONArray 执行此操作的正确方法,或者如果我遇到了限制。)
我已简化此示例并确认它与上述文档不匹配。
pipeline, err := bson.ParseExtJSONArray(`[
{ "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }
]`)
cursor, err := receivedFromResponseQueue.Aggregate(ctx, pipeline)
Run Code Online (Sandbox Code Playgroud)
以下内容在 mongo shell 中不起作用。(这并不奇怪,因为它会自动转换为 ISODate() 格式)
db.getCollection('received_from_response_queue').aggregate([
{ "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }
])
Run Code Online (Sandbox Code Playgroud)
但这在 mongo shell 中确实有效。
db.getCollection('received_from_response_queue').aggregate([
{ "$match": { "dateTimeGMT": ISODate("2018-08-22T09:29:25.000Z") } }
])
Run Code Online (Sandbox Code Playgroud)
但这会在“pipeline”中返回一个空数组。(因为 ParseExtJSONArray 不处理 JavaScript)
pipeline, err := bson.ParseExtJSONArray(`[
{ "$match": { "dateTimeGMT":ISODate("2018-08-22T09:29:25.000Z") } …Run Code Online (Sandbox Code Playgroud) mongodb ×7
go ×3
mongo-go ×2
bson ×1
mgo ×1
mongo-shell ×1
mongoengine ×1
node.js ×1
pymongo ×1
python ×1
python-2.7 ×1
recursion ×1