MongoError:无法在地图集中对管理员进行查询 [英] MongoError: cannot do queries on admin in atlas
问题描述
我刚刚在 Atlas 上设置了一个免费的 MongoDB.我添加了一个名为react-project"的数据库,我正尝试使用 mongoose 连接到该数据库.这是我的连接字符串:
I just set up a free MongoDB on Atlas. I added a database called 'react-project' that I'm trying to connect to using mongoose. Here is my connection string:
mongoose.connect('mongodb+srv://myUser:myPassword@myCluster.mongodb.net/react-project');
如您所见,在查询结束时,我指定了要连接的数据库.我可以很好地连接到 mongodb 实例,但是当我尝试执行操作(例如 Model.find())时,出现以下 MongoError:
As you can see, at the end of the query I specify the database I want to connect to. I can connect to the mongodb instance just fine, but when I try and do an operation (such as Model.find()), I get the following MongoError:
MongoError: cannot do raw queries on admin in atlas
这让我相信它没有连接到react-project"数据库,而是连接到admin"数据库.
This leads me to believe that it is not connecting to the 'react-project' database, but is connecting to the 'admin' database instead.
为什么会发生这种情况以及如何连接到正确的数据库?
Why is this happening and how can I connect to the correct database?
这是我正在运行的代码,它给了我错误:
Here is the code I'm running that is giving me the error:
var productSchema = mongoose.Schema({
productId: Number,
img: String,
price: Number,
title: String,
type: String
});
var Product = mongoose.model('Product', productSchema);
// Return all products
Product.find(function(error,result) {
if (error) return console.error(error);
console.log(result);
});
推荐答案
我遇到了完全相同的问题.好像是新的.
I had the exact same issue. Seems like its a new one.
这是我现在可以使用的连接字符串
Heres my connection string that now works
mongoose.connect('mongodb://USERNAME:密码@myShard-shard-00-00-lbofd.mongodb.net:27017,myShard-shard-00-01-lbofd.mongodb.net:27017,myShard-shard-00-02-lbofd.mongodb.net:27017/MYDBNAME?ssl=true&replicaSet=myShard-shard-0&authSource=admin');
mongoose.connect('mongodb://USERNAME:PASSWORD@myShard-shard-00-00-lbofd.mongodb.net:27017,myShard-shard-00-01-lbofd.mongodb.net:27017,myShard-shard-00-02-lbofd.mongodb.net:27017/MYDBNAME?ssl=true&replicaSet=myShard-shard-0&authSource=admin');
为了创建一个新的数据库,我使用了 MongoDB Compass.从 Atlas 站点复制连接字符串(我相信较长的一个 - 3.4),当您打开 mongodb compass 时,它会识别出剪贴板 (OSX) 中有一个连接字符串,并允许您使用 Compass 填充连接简单的点击.你可以从那里创建一个新的数据库(点击左上角的 MyCluster - 然后创建数据库.将新数据库的名称放在我的连接字符串中的粗体 MYDBNAME 中.
To create a new DB I used MongoDB Compass. Copy the connection string from the Atlas site(the longer one - 3.4 I believe), when you open mongodb compass it will recognize that there is a connection string in your clipboard (OSX) and will allow you to populate the connection for Compass with a simple click. You can create a new Database from there (Click on MyCluster at top left - then Create Database. Put the name of the new db in the bolded MYDBNAME in my connection string.
确实需要刷新几次才能看到新数据.
It did take a few refreshes to see new data.
我和你有很多相同的连接字符串(数据库名称/分片不同等) - 我可以 POST 但不能 GET.POSTS 没有创建错误(但我找不到我发布的数据)并且 GET 抛出了与您相同的错误.
I had much the same connection string as you (db name/shard different etc) - I was able to POST but not GET. POSTS didnt create error (but I could not find data I posted) and GET threw the same error as you got.
这篇关于MongoError:无法在地图集中对管理员进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!