How to create a user in MongoDB - mongodb

I'm using the latest version of the driver and MongoDB database 2.6 and I used to create users using the following code:
MongoUser _user1 = new MongoUser("username", "password", false);
MongoDatabase.AddUser(_user1);
and now it is saying MongoDatabase.AddUser() is deprecated by showing the following information:
...is obsolete: Use the new user management command 'createUser' or
'updateUser'."
Where is this new user management command? How do I create a user using the new MongoDB C# driver?

For those interested in creating a user with the C# v2.0.1 driver and MongoDB v3.0.6 use the following:
var client = new MongoClient(connectionString);
var database = client.GetDatabase("database_to_create_user_in");
var user = new BsonDocument { { "createUser", "fred" }, { "pwd", "some_secure_password" }, { "roles", new BsonArray { new BsonDocument { { "role", "read" }, { "db", "database_to_create_user_in" } } } } };
await database.RunCommandAsync<BsonDocument>(user);

I'm using MongoDB 2.6.2 and latest C# driver 1.9.2.
This is how you can add new users with 'createUser':
public static void AddUser(string user, string password, string[] roles)
{
var database = GetDatabase();
var command = new CommandDocument { { "createUser", user}, { "pwd", password }, { "roles", new BsonArray(roles) } };
var result = database.RunCommand(command);
}

I did a search for 'createUser' on the latest code on the GitHub repository:
https://github.com/mongodb/mongo-csharp-driver/search?q=createUser&ref=cmdform
At the time of writing, the only reference to 'createUser' is here
The method has been marked as obsolete however in this commit
However; on closer inspection of the code, I see this:
if (_server.RequestConnection.ServerInstance.Supports(FeatureId.UserManagementCommands))
{
AddUserWithUserManagementCommands(user);
}
else
{
AddUserWithInsert(user);
}
So, it is calling the required method under the hood

For me I struggled with the roles. I wanted to add an admin (initial user) to the mongodb. My working solution looks like that.
private static bool CreateAdminUser(string databaseUser, string databasePassword)
{
try
{
var databaseName = "admin";
var user = new CommandDocument
{
{ "createUser", databaseUser },
{ "pwd", databasePassword },
{
"roles", new BsonArray
{
new BsonDocument { { "role", "readWriteAnyDatabase" }, { "db", databaseName } },
new BsonDocument { { "role", "userAdminAnyDatabase" }, { "db", databaseName } },
new BsonDocument { { "role", "dbAdminAnyDatabase" }, { "db", databaseName } }
}
}
};
new MongoClient().GetServer().GetDatabase(databaseName).RunCommand(user);
return true;
}
catch
{
return false;
}
}

Related

mongoose update inside array object

How to update mongodb based
image
need to update in_state Object based on testid and user_id
code
const test = await usertest.update(
{ "testid" :"oOEbG3ycsl5ZIPrFNk172SVma0zTwD6xHvpUM4JWKAj9fg18nRCtq1B0XLdYeuiQh","user_id":"63297394d10aa70d52708a4c"},
{
$set: {
'testdata.1.in_state': in_state
}
}
)
how can achieve this
Try referencing the testdata elements with $:
const test = await usertest.update(
{
testid: 'oOEbG3ycsl5ZIPrFNk172SVma0zTwD6xHvpUM4JWKAj9fg18nRCtq1B0XLdYeuiQh',
user_id: '63297394d10aa70d52708a4c',
},
{
$set: {
'testdata.$.in_state': in_state,
},
}
);

mongoose When I Use update it updates Nothing with status 200(success)

I use update Query for push some data in array in Mongodb and I use mongoose in nodeJs.Pplease anyone can help out from this.
Model Schema :
var mongoose = require('mongoose')
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt')
var schema = new Schema({
email: { type: String, require: true },
username: { type: String, require: true },
password: { type: String, require: true },
creation_dt: { type: String, require: true },
tasks : []
});
module.exports = mongoose.model('User',schema)
So I use this schema and I want to push data in tasks array and here is my route code for pushing data.
Route For Update Data in Tasks:
router.post("/newTask", isValidUser, (req, res) => {
addToDataBase(req, res);
});
async function addToDataBase(req, res) {
var dataa = {
pName: req.body.pName,
pTitle: req.body.pTitle,
pStartTime: req.body.pStartTime,
pEndTime: req.body.pEndTime,
pSessionTime: req.body.pSessionTime,
};
var usr = new User(req.user);
usr.update({ email: req.user.email }, { $push: { tasks: dataa } });
console.log(req.user.email);
try {
doc = await usr.save();
return res.status(201).json(doc);
} catch (err) {
return res.status(501).json(err);
}
}
Here I create a async function and call that function in route but when I post data using postman it response with status code 200(success) but it updates nothing in my database.
Output screenshot:
as you can see in this image task : [].. it updates nothing in that array but status is success
I don't know why is this happening.
You can achieve this task easier using findOneAndUpdate method.
router.put("/users", isValidUser, async (req, res) => {
var data = {
pName: req.body.pName,
pTitle: req.body.pTitle,
pStartTime: req.body.pStartTime,
pEndTime: req.body.pEndTime,
pSessionTime: req.body.pSessionTime,
};
try {
const user = await User.findOneAndUpdate(
{ email: req.user.email },
{
$push: {
tasks: data,
},
},
{ new: true }
);
if (!user) {
return res.status(404).send("User with email not found");
}
res.send(user);
} catch (err) {
console.log(err);
res.status(500).send("Something went wrong");
}
});
Also I strongly suggest using raw / JSON data for request body, that's how most ui libraries (reactjs, angular) send data.
To be able to parse json data, you need to add the following line to your main file before using routes.
app.use(express.json());
TEST
Existing user:
{
"tasks": [],
"_id": "5e8b349dc285884b64b6b167",
"email": "test#gmail.com",
"username": "Kirtan",
"password": "123213",
"creation_dt": "2020-04-06T14:21:40",
"__v": 0
}
Request body:
{
"pName": "pName 1",
"pTitle": "pTitle 1",
"pStartTime": "pStartTime 1",
"pEndTime": "pEndTime 1",
"pSessionTime": "pSessionTime 1"
}
Response:
{
"tasks": [
{
"pName": "pName 1",
"pTitle": "pTitle 1",
"pStartTime": "pStartTime 1",
"pEndTime": "pEndTime 1",
"pSessionTime": "pSessionTime 1"
}
],
"_id": "5e8b349dc285884b64b6b167",
"email": "test#gmail.com",
"username": "Kirtan",
"password": "123213",
"creation_dt": "2020-04-06T14:21:40",
"__v": 0
}
Also as a side note, you had better to create unique indexes on username and email fields. This can be done applying unique: true option in the schema, but better to create these unique indexes at mongodb shell like this:
db.users.createIndex( { "email": 1 }, { unique: true } );
db.users.createIndex( { "username": 1 }, { unique: true } );
It's been awhile since I've done mongoose, but I'm pretty sure <model>.update() also actively updates the record in Mongo.
You use .update() when you want to update an existing record in Mongo, but you are instantiating a new User model (i.e. creating a new user)
try the following code instead for a NEW USER:
router.post('/newTask', isValidUser, (req, res) => {
addToDataBase(req,res)
})
async function addToDataBase(req, res) {
var dataa = {
pName: req.body.pName,
pTitle: req.body.pTitle,
pStartTime: req.body.pStartTime,
pEndTime: req.body.pEndTime,
pSessionTime: req.body.pSessionTime
}
// email field is already in `req.user`
var usr = new User({ ...req.user, tasks: [dataa] });
console.log(req.user.email);
try {
await usr.save();
return res.status(201).json(doc);
}
catch (err) {
return res.status(501).json(err);
}
}
Now, if you wanted to update an existing record :
router.post('/newTask', isValidUser, (req, res) => {
addToDataBase(req,res)
})
async function addToDataBase(req, res) {
var dataa = {
pName: req.body.pName,
pTitle: req.body.pTitle,
pStartTime: req.body.pStartTime,
pEndTime: req.body.pEndTime,
pSessionTime: req.body.pSessionTime
}
try {
await usr. updateOne({ email : req.user.email}, { $push: { tasks: dataa } });
return res.status(201).json(doc);
}
catch (err) {
return res.status(501).json(err);
}
}
For more info read: https://mongoosejs.com/docs/documents.html

Building MongoDB query with conditions

I need to build a MongoDB query by pushing a new language if it does not exist in the array already. But if it exists I get an error this '$push' is empty. It is correct.
My question is how to build the query adding $push only when it is necessary?
let pushNewLanguage = {};
if (!profile.languages || (profile.languages && !profile.languages.find(l => l === languageId))) {
pushNewLanguage = { languages: languageId };
}
const profileUpdate = await
Profiles.rawCollection().update(
{ userId: this.userId },
{
$inc: { countPublishedPoems: 1 },
$push: pushNewLanguage
}
);
Remove the conditional logic and use $addtoSet instead of $push.
$addToSet will only add the item if it doesn’t exist already.
const profileUpdate = await
Profiles.rawCollection().update(
{ userId: this.userId },
{
$inc: { countPublishedPoems: 1 },
$addToSet: { languages: languageId }
}
);
Since you are writing Javascript, you can create a "base" update object, and then add the $push property if you need:
const update = {
$inc: { countPublishedPoems: 1 }
}
if (!profile.languages || (profile.languages && !profile.languages.find(l => l === languageId))) {
update["$push"] = { languages: languageId };
}
const profileUpdate = await
Profiles.rawCollection().update(
{ userId: this.userId },
update
);

Change type of value in collection using update

I am looking for a way, how to update multiple documents in MongoDB. I want to modify similar structure like this one:
{[
"_id": 'mongo_id',
"name": "Name"
]}
to the structure like this, basically just change string attribute to object attribute with string property :
{
"_id": 'mongo_id',
"name": {
"type_1": "Name",
"type_2": ""
}
}
Is there a way how to do it in single mongo query or I have to create some kind of worker for example in node.js?
If you do not have any schemas involved to put constrains on your collections or if you have and name is defined as mixed type (from mongoose types as an example) then you can do whatever you want to any of the properties other than _id.
for example this update will change name to the object you want:
db.getCollection('<collectionName>').update({
_id: "mongo_id")
}, {
name: {
"type_1": "Name",
"type_2": ""
}
})
It looks like the best solution is create little worker to get all documents and update them in collection. I used node.js and mongodb npm package to create worker similar to this one:
var mongo = requiere('mongodb');
mongo.connect(process.env.MONGO_URL, function(err, database) {
if (err) {
throw err;
}
db = database;
db.collection('<collectionName>').find({}).toArray(function(err, array) {
if (err) {
console.log(err);
return process.exit(1);
}
console.log('length:', array.length);
var promises = [];
array.forEach(item => {
promises.push(db.collection('<collectionName>').update(
{
_id: item._id
},
{
'$set': {
'name': {
'type_1': item.name,
'type_2': '',
}
}
}))
});
return Promise.all(promises).then(function() {
console.log('Done');
return process.exit(0);
}).catch(function(err) {
console.log('err:', err);
return process.exit(1);
});
});
});

Trouble in inserting sub-documents using mongoose

I created an Enterprise database using mongoose in node-express project.Now I need to add employee sub document in the enterprise_employee field of the enterprise database, but it throws an error.
Following code snippet is my schema
var mongoose= require('mongoose');
var Enterprise= new mongoose.Schema({
enterprise_id:{
type:String
},
enterprise_name:{
type:String
},
enterprise_email:{
type:String
},
enterprise_employee: [{employee_id:Number, employee_name:String}]
});
module.exports={
Enterprise:Enterprise
};
This code snippet is the route for adding employee sub-document
var mongoose = require('mongoose');
var Enterprise = mongoose.model('Enterprise_gpy');
var addEmployee = function(req, res){
Enterprise.findOne({"enterprise_id":req.body.enterprise_id},function(err, res){
if(err){
console.log('NO SUCH ORGANISATION');
res.json(err);
} else {
Enterprise.enterprise_employee.push({
"employee_id": req.body.employee_id,
"employee_name":req.body.employee_name
});
}
});
}
module.exports={
addEmployee:addEmployee
};
This the error thrown
throw er; // Unhandled 'error' event
^ TypeError: Cannot read property 'push' of undefined
Seems like what you need is an update operation that uses the $push operator to add the elements to the array field. The following example demonstrates this:
Enterprise.findOneAndUpdate(
{ "enterprise_id": req.body.enterprise_id },
{
"$push": {
"enterprise_employee": {
"employee_id": req.body.employee_id,
"employee_name":req.body.employee_name
}
}
},
{ "new": true }, // return the modified document
function(err, enterprise) {
if (err) {
console.log('NO SUCH ORGANISATION');
res.json(err);
} else {
console.log(enterprise); // modified document
}
}
);
I think this is because your schema needs to define the enterprise_employee as an array. You have to explicitly tell Mongoose that it should be an 'Array' type.
Try this:
enterprise_employee: {
type: Array,
fields: [
{
employee_id: String,
employee_name: String
}
]
}