MongoError: cannot do raw queries on admin in atlas i tried to run :
mongoose
.connect(
'mongodb srv://yonco:[email protected]/?retryWrites=true&w=majority'
)
.then(result => {
User.findOne().then(user => {
if (!user) {
const user = new User({
name: 'yonc',
email: '[email protected]',
cart: {
items: []
}
});
user.save();
}
});
app.listen(3000);
})
.catch(err => {
console.log(err);
});
i actully copied the source code from the course im doing but the error remain
CodePudding user response:
Your string should look like this:
mongodb://<user>:<password>@cluster0-shard-00-00-rnmsm.mongodb.net:27017,cluster0-shard-00-01-rnmsm.mongodb.net:27017,cluster0-shard-00-02-rnmsm.mongodb.net:27017/test?ssl=true&replicaSet=cluster0-shard-0&authSource=admin&retryWrites=true&w=majority
Use your username and password correctly.
I would suggest make env for connection to MongoDB url as it contains user details. env docs : https://www.twilio.com/blog/working-with-environment-variables-in-node-js-html
Hope this helps! Please upvote if you like.