MongoDB and Mongoose - Create and Save a Record of a Model

Tell us what’s happening:
i have some problem with the code, i try follow the hint but always error “MongoWriteConcernError: No write concern mode named 'majority ’ found in replica set configuration”.

this my code :

require('dotenv').config();
const mySecret = process.env['MONGO_URI'];

// connected mongoose
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

// declare mongoose
// create model
const Schema = mongoose.Schema;


const personSchema = new Schema({
  nama: { type: String, required: true },
  umur: Number,
  minuman: [String]
});

const Person = mongoose.model("Person", personSchema);

// end create model

const createAndSavePerson = function(done) {
 var kocak = new Person({ nama: "barbar", umur: 21, minuman: ["anggur", "air", "susu"] });

  kocak.save(function(err, data) {
    if (err) return console.error(err);
    done(null, data)
  });
};

Your project link(s)

solution: boilerplate-mongomongoose - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge: MongoDB and Mongoose - Create and Save a Record of a Model

Link to the challenge:

Try like this

module.exports = mongoose.model('User', userSchema);

for example, and look result and if still error may be instead of using environment variable try direcly using without environment variable

however, i am not sure but sometimes i recieved some errors and solved them without using environment variables.

Environment Variables Just Does Not Like Me :grinning: I dont know Why :joy:

You changed the personSchema it has to be the one you made in the previous challenge. If I fix that your code passes the tests.

I don’t think the error you are getting is related to this. But I might suggest you fix the Schema and then delete the collection and try again.

If you still get the same error I would suggest you make a new cluster and select a different region and/or provider and see if that helps.


I can’t say I fully understand how the read/write concern with replica sets works. There is a system in place that I’m not going to try to explain but below are some related docs in case you want to try and better understand the error.

You might be able to bypass it by removing &w=majority from the connection string (or using &w=1 might work as well). But that doesn’t seem like a good idea if it makes the DB less reliable. Again, I can’t say I really took the time to fully understand how this works.

Anyway, my best guess (as I have never seen this error) is that the cluster is the issue or the time it is taking to acknowledge is too slow.

it’s work i’m replace env ‘&w=majority’ to &w=1 thanks sir

thanks sir for reply, my problem had been solved. the env have to change from &w=majority to &w=1

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.