I've a question on db: { safe: true } - which is being passed as an option to mongoose.connect in my nodeJS and MongoDB project.
I have the impression that this option is obsolete. Is that the case ?
If yes, what was it replaced by?
If not, where can I find the details of this option?
Knowing that I spent the afternoon looking for this information.
Many thanks in advance to you!
This behavior, "SafeMode" appears to be obsolete. This is the equivalent of WriteConcern.Acknowledged, which is the default now.
You can manage this behavior on a transaction basis now, read more about WriteConcern here: https://docs.mongodb.com/manual/reference/write-concern/
Related
Ok so I just started looking into databases yesterday
index.js: https://hatebin.com/pvqnubsrrs
database.js: https://hatebin.com/csgvmvolfz
I just want it to insert into the DB,
how would I accomplish that?
Someone told me it's creating a new DB, but when I run the command it says the table doesn't exist
The error says:
Error: SequelizeDatabaseError: SQLITE_ERROR: no such table: PointsSystems
However, I cannot find a spot in the code where I use PointsSystems, I only use PointsSystem, without the s
Edit: Thank you SigFried for making this more readable.
The error says PointSystems because, by default, Sequelize will pluralize the name you gave to the table. Your code looks O.K, the only missing part and the solution to your problem is the await sequelize.sync({ force: true })}. I made a little example to show you how it's done in a very basic way.
You should be able to find out why the sequelize.sync() methods is needed if you read this.
I got an old nodeJs MongoDb project.
In the code the save function has as option:
save({safe: {j: true}}, (e, o) => {})
But "safe" is depreacted. It is indicated to use the option "w" with in the warning the link for more details (https://mongoosejs.com/docs/api.html#model_Model-save).
Unfortunately I do not have more details on the use of the w option. Could you guide me on this subject?
Knowing that I spent the afternoon looking for this information.
Many thanks in advance to you!
I guess you could simply do save({"w": 1}); use 1/0 depending upon your requirement.
1: Requests acknowledgment that the write operation has propagated to the standalone mongod or the primary in a replica set
0: Requests no acknowledgment of the write operation. However, w: 0 may return information about socket exceptions and networking errors to the application.
How to set Hybris db transaction timeout using oracle database?
I have tried the following code but no effect, thanks in advance.
TransactionTemplate template = new TransactionTemplate(manager);
template.setTimeout(1);
You can set in local.properties db.connectionparam.oracle.jdbc.ReadTimeout or db.connectionparam.oracle.net.READ_TIMEOUT (both in millisecond).
Check this post for the difference : https://stackoverflow.com/a/18513472/1140748
To explain a bit, in the platform, in AbstractTenant you have a method extractCustomDBParams that checks for key starting with db.connectionparam .
As you know, in mongoose, we can remove all users with age 30 like this:
User.find({age: 30}).remove(callback);
Now, replace find() with findOne(), and I think it should remove only 1 user:
User.findOne({age: 30}).remove(callback);
oh, not as I expected, the code above also remove ALL instead of ONE
So, why findOne().remove() remove ALL instead of ONE? Is that a bug or a feature and why?
Thanks in advance!
P/S: I know findOneAndRemove() would remove one user for me, but in this question I want to understand findOne().remove()
I have reported this question to mongoose team, and got a reply:
https://github.com/LearnBoost/mongoose/issues/1851#issuecomment-31355346
Here's the message from aheckmann
"that's a good catch. findOne just sets the command name to run, remove() changes it back to a rice command but no limit was ever set. We should probably change that in 3.9 so that findOne sets
the limit as well."
Both find and findOne returns mongoose Query objects which only contains information about the model and the specified query. It's not taking into account findOne which is applied first in the callback. What you expect to happen is to have options be set like this User.findOne({age: 30}, null, {limit: 1}).remove() as this would only remove one and you could argue that this is a bug, but that depends on the usage. Like you have already pointed out, the right way to go is to use findOneAndRemove().
I'm kind of a noob but wouldn't you need to put your remove in the callback because this is an asynchronous function? Try something like:
User.findOne({age: 30}, function(err, user){
user.remove()
})
I have a mongoose setup which involves an embedded-schema, lets say: A Blogpost with embedded comments. Comments can be edited by the original publisher as well as by an editor/admin. After adding / editing a comment the entire blogpost is saved.
I have some custom mongoose's 'pre' middleware set up on the embedded comment-schema which automatically sets the lasteditdate for that particular comment.
The thing is that 'pre' is called on EVERY comment on the blogpost, since I call save() on the blogpost. (For other reasons I need to do it like this) . Therefore, I need a way to check which comments have changed (or are new) since they were last saved (as part of the Blogpost overall save())
The questio: how to check in 'pre' whether a comment has changed or not? Obviously calling this.isNew isn't sufficient, since comments could be edited (i.e: aren't new) as well.
Is there any isDirty or similar that I'm overlooking?
For version 3.x
if(doc.isModified()){
// do stuff
}
In Mongoose you can use the Document method isModified(#STRING).
The most recent documentation for the method can be found here.
So to check a specific property with doc.isModified you can do this:
doc.comments[4].message = "Hi, I've made an edit to my post";
// inside pre hook
if ( this.isModified('comments') ) {
// do something
}
If you want to check a specific comment you can do that with the following notation this.isModified('comments.0.message')
Since the argument takes a string if you needed to know specifically which comment was modified you could loop through each comment and run this.isModified('comments['+i+']message')
You may use the modified getter:
if (doc.modified) {
// :)
}
This may be relevant to Mongoose users circa mid-2020 who are seeing this error:
"errorType": "TypeError",
"errorMessage": "Cannot set property 'isDirty' of null",
Upgrade to the latest version of Mongoose to fix it.
https://github.com/Automattic/mongoose/issues/8719