I am trying to create unique passwords for every document in a MongoDB collection. Although the function works, it creates the same password for each user.
Here is the code I am currently using:
function createPasswords(){
db.collection('users').updateMany({}, {$set:{password: GeneratePassword(12, false)}});
}
I expected the GeneratePassword function to be run for each document but it obviously only runs once as the result is the same random password for each user in the collection.
My question is, in this case, how might I create unique passwords for each user at once using updateMany.
Incidentally, the GeneratePassword function is not a custom function but instead calls the password-generator package.
Thanks in advance!
UPDATE
I tried the following code based on the answer by turivishal below.
db.collection('users').updateMany({},[{
$set: { updated: 'true',
password: {
$function: {
body: function() {
$function: {
function passGen(param1, param2) {
return GeneratePassword(param1,param2)
}
return passGen(12, false);
}
},
args: [],
lang: "js"
}
}
}
}]);
This produced the following error:
MongoServerError: Invalid $set :: caused by :: The body function must be specified.
If anyone can spot what is going wrong here I'd much appreciate the guidance.
The update() / updateMany() method in MongoDB is a single write operation, modifies multiple documents, the modification of each document is atomic. (we can not say the whole operation is atomic as per docs), but it will not update the different values in each document of collection.
You have to update it one by one or loop through or you can try an update with aggregation pipeline query starting from MongoDB 4.2, it allows for a more expressive update statement and $function starting from MongoDB 4.4, defines a custom aggregation function or expression in JavaScript.
I would suggest this query only if this is a one-time process because it will impact query speed and performance, and as per operator support, this query will support from MongoDB 4.4 or above versions.
db.collection('users').updateMany(
{},
[{
$set: {
password: {
$function: {
body: function() {
// write your generate password method here
function GeneratePassword(param1, param2) {
// ...
}
// this will call internal method and return password
return GeneratePassword(12, false);
},
args: [],
lang: "js"
}
}
}
}]
)
Playground
Related
Hello there, a quick MongoDB mixed with some Discord knowledge question:
So currently, I want a formation for my MongoDB Document similar to the following:
channels: {
utility:{
suggestions: String
},
logging: {
main: String,
channel: {
channelCreate: String,
channelDelete: String,
channelUpdate: String,
},
role: {
roleCreate: String,
roleDelete: String,
roleUpdate: String,
}
}
This saves channel IDs so users can decide where each event will be logged. I have this set up in the schema and all good, but when I do findOneAndUpdate I don't know how to edit a single field; for example, let's say I want to edit roleDelete which is inside channels.logging.role how would I do that? because doing
await doc.updateOne({channels:{logging:{role:{roleDelete: IDHERE}}}});
It does not work. In fact, it screws everything up and replaces everything within channels to the value given, so how would I go around actually updating ONE value without messing with everything else? Thank you so much for your attention and participation.
This is using NodeJS Mongoose NPM Package btw.
you need to use $set operator. you can find details on https://docs.mongodb.com/manual/reference/operator/update/set/index.html
doc.updateOne({ _id: ID }, {
$set: {
channels.logging.role.roleDelete: IDHERE
}
}
So I solved this by doing the following:
await doc.updateOne({ 'channels.logging.role.roleDelete': IDHERE}, { new: true, upsert: true, setDefaultsOnInsert: true });
This updated the value if it existed and created it if it didn't exist. Using the above methods uses $set internally. (Read more here)
Feel free to ask if I didn't make myself clear
I am writing code in nodejs/MongoDB and am countering this particular issue which I was hoping to get help with:
I have various schemas defined within my Models and I note that for each of these MongoDB automatically populates a unique id, which is represented by the _id field.
However, I am interested in creating a customized ID, which is basically an integer that auto-increments. I have done so by utilizing the 'mongoose-auto-increment' npm package. For example, in my UserSchema:
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", UserSchema);
autoIncrement.initialize(mongoose.connection);
UserSchema.plugin(autoIncrement.plugin, {
model: 'UserSchema',
field: 'user_id',
startAt: 1,
incrementBy: 1
});
To speed up my application, I have a seeds.js file which aims to load a bunch of data upon application initialization. However, to make this fully functional, I need a way to access my models and reference them over to other models (for cases when there is a one-to-one and one-to-many relationship). Since the mongoDB default _id is extremely long and there is no way to get the result unless I am actually on the html page and can use the req.params.id function, I have been trying to use mongoDB's findOne function to do this without success.
For example:
var myDocument = User.findOne({user_id: {$type: 25}});
if (myDocument) {
var myName = myDocument.user_id;
console.log(myName);
}
However, the result is always 'undefined' even though I know there is a User model saved in my database with a user_id of 25.
Any help would be much appreciated :)
User.findOne({ user_id: 25 }).exec(function (err, record) {
if (err) {
console.log(err);
} else {
console.log(record);
}
});
You need to undestand the nature of Node.js.
Node.js runs in async nature so you can't get the result here.
You need to do with other ways
like:
use callback
use promise
use async/await(ES8)
Try this:
User.findOne({user_id: {$type: 25}}, function (err, myDocument) {
if (myDocument) {
var myName = myDocument.user_id;
console.log(myName);
} else {
console.log(err);
}
});
In a failed attempt learning exercise to get validators to work with 'document.update', I came across something I don't understand.
I know now that it doesn't work, but one of the things I tried was setting my options to {runValidators:true, context:'query'}. In my validator function, I tried console.logging (this), with and without the context:"query" option.
There was no difference. I received a large object (is this called the 'query object'?) This seems to go against what I read here.
In the color validation function above, this refers to the document being validated when using document validation. However, when running update validators, the document being updated may not be in the server's memory, so by default the value of this is not defined.
It was not undefined , even without the context option.
I even tried making it an arrow function to see if the lexical this was any different. In that case, this was undefined, but again, changing the context option did not make a difference. (I'm still learning, so I don't know if that part is relevant).
in the model:
let Property = mongoose.model('Property', {
name: {type:String, required:true},
occupancy: {type:String},
maxTenants: Number,
tenants: [{ type:mongoose.Schema.Types.ObjectId, ref: 'Tenant', validate: [checkMaxTenants, "Maximum tenants exceeded for this property. Tenant not added."]}]
});
function checkMaxTenants(val){
console.log("this",this);
// return this.tenants.length <= this.maxTenants;
return true;
}
and in the route:
property.update({$set: {tenants:property.tenants}},{new:true,runValidators:true,context:'query'}, function(err,savedProperty){
Anything to help me better understand the discrepancy between what I think I'm reading and what I see would be great!
At the outset, let's be clear that validators are of two types: document validators and update validators (maybe you know this already, but the snippet you posted updates a document, whereas the issue you mention relates to document validation upon save).
There was no difference. I received a large object (is this called the 'query object'?) This seems to go against what I read here.
Document validators are run when you run save on documents as mentioned in the docs.
Validation is middleware. Mongoose registers validation as a pre('save') hook on every schema by default.
Or you can call it manually with .validate()
You can manually run validation using doc.validate(callback) or doc.validateSync()
Update validators are run for update operations
In the above examples, you learned about document validation. Mongoose also supports validation for update() and findOneAndUpdate() operations.
This can be illustrated with the following snippet. For convenience I have changed the type of tenants to a simple integer array, but that shouldn't matter for the purpose of our discussion.
// "use strict";
const mongoose = require('mongoose');
const assert = require('assert');
const Schema = mongoose.Schema;
let Property = mongoose.model('Property', {
name: { type: String, required: true },
occupancy: { type:String },
maxTenants: Number,
tenants: [
{
type: Number,
ref: 'Tenant',
validate: {
validator: checkMaxTenants,
message: "Maximum tenants exceeded for this property. Tenant not added."
}
}
]
});
function checkMaxTenants (val) {
console.log("this", this);
// return this.tenants.length <= this.maxTenants;
return true;
}
mongoose.Promise = global.Promise;
mongoose.createConnection('mongodb://localhost/myapp', {
useMongoClient: true,
}).then(function(db) {
const property = new Property({ name: 'foo', occupancy: 'bar', tenants: [1] });
property.update(
{ $set: { tenants: [2, 3] } },
{
new: true,
runValidators: true,
// context: 'query'
},
function(err, savedProperty) {
}
)
// property.save();
});
Above code with trigger a update validation not document validation
To see document validation in action uncomment property.save() and comment the update operation.
You'll notice that the value of this will be the property document.
this { name: 'foo',
occupancy: 'bar',
_id: 598e9d72992907120a99a367,
tenants: [ 1 ] }
Comment the save, uncomment back the update operation and you'll see the large object you mentioned.
Now the large object you got, you may not have realised, is the global object when you didn't set context: 'query' and the query object when you set the context.
This can be explained at this line in mongoose's source. When no context was set, mongoose sets the scope to null. And then here the .call is called with the scope.
Now, in non strict mode, when .call is called with null, this is replaced with the global object. So check contents of the large object you got. When context is not set, it would be a global object and not the query object. You can add "use strict"; and see that null will be logged. (The snippet posted can verify this for you). You can verify that you got a query object by running instanceof mongoose.Query against this.
Hope this helps you understand things better.
This Meteor server code tries to copy the newly created property _id into a sub document but failed to do so.
How can it be done?
edit:
The code uses matb33:collection-hooks.
MyCollection.after.insert(function(userId, doc) {
if (doc.element === 'myString') {
doc.values[0]._id = doc._id;
}
});
Mutating the doc in the after hooks of matb33:collection-hooks will not cause additional queries to be run. You will need to explicitly update the document if you wish to do so.
However, in this particular case, if you really need the duplicate _id in the document, you could generate an _id and specify it when inserting the document.
You can probably use MyCollection._makeNewID() method, as this API has not changed for a few years and it is what the Mongo package uses internally.
const _id = MyCollection._makeNewID();
const doc = {
_id,
values: [
{
_id,
...
}, {
...
}
]
};
MyCollection.insert(doc);
In my database, I have a field called 'fruits' which is a simple array. When inserting elements in this array, I use $addToSet to only insert elements that do not exist already in this array.
I want to know whether or not the element I insert really modified this field. However, the docModified parameter in the callback always returns 1 even when I try to add an element that already exists.
model.update (
{ username: username }, // find the document
{ $addToSet : { fruits: "apple" } }, // add "apple" to fruits field
function (err, docModified) {
console.log(docModified);
// !PROBLEM: this will print "1" no matter what
}
);
Does anyone know why? Thanks a lot! (btw I'm using Mongoose)
The current method implementations in mongoose use the legacy write concern API to determine the count of modified documents. As you note, even if there is no actual change to the content such as an $addToSet operation that does not add a new member to the set, the modified count will be returned.
As long as your MongoDB server version is recent enough ( needs to be MongoDB 2.6 or greater ) and your mongoose version is recent enough an bundles a recent mongodb driver, then you can get the correct numbers from the Bulk Operations API responses.
To do this with mongoose you call the .collection accessor on the model, which returns a native driver collection object:
var bulk = Model.collection.initializeOrderedBulkOp();
bulk.find({ username: username })
.updateOne({ $addToSet : { fruits: "apple" } });
bulk.execute(function(err,result) {
if (err) throw err;
console.log( JSON.stringify( result, undefined, 4 ) );
})
The "result" that is returned is an object conforming to the BulkWriteResult() specification, which more or less will look like this:
{
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 3,
"nModified" : 3,
"nRemoved" : 1,
"upserted" : [ ]
}
But specifically, where you use $addToSet and the member already exists, the response will contain "nMatched": 1 and "nModified": 0, which is the result you want to see, confirming that nothing was in fact added to the set.
In the MongoDB 2.6 shell, all of the update and insert methods try to use this API where it is available and only fallback to the legacy implementation when connecting to an older server. So If you did this in a modern shell with a modern server you would see the same results.
Hopefully mongoose itself will be altered to also use these methods where available and provide access to the same response. It seems to be the way of the future, but the codebase is yet to catch up and utilize this.
Note: The one thing to be careful of when using any of the native driver methods after accessing the collection object is to make sure that mongoose has an active connection to the database at the time this is called. Mongoose hides this by queuing up any request until a connection is actually established.
So if you are going straight to the collection object methods, then you might want to make sure you are waiting for the "open" event on the connection somewhere before that code executes.
"number affected is the number of docs updated, even if the new values are identical. this comes straight from mongo." I got this from this forum post: https://github.com/LearnBoost/mongoose/issues/867
This means you'll have to come up with a different way to determine if the element was missing from the array before you update. I would recommend pull all the documents and iterating through them before the update. Obviously it's not ideal, but i don't think there's any other way to do it.
Hope this helps.