Options of mongoose save function - node.js

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.

Related

Consecutive calls to updateOne of mongodb: 3rd one does not work

I receive 3 post calls from client, let say in a second, and with nodejs-mongodb immediately(without any pause, sleep, etc) I try to insert the data that is posted in database using updateOne. All data is new, so in every call, insert would happen.
Here is the code (js):
const myCollection = mydb.collection("mydata")
myCollection.updateOne({name:req.data.name},{$set:{name:req.data.name, data:req.data.data}}, {upsert:true}, function(err, result) {console.log("UPDATEONE err: "+err)})
When I call just 1 time this updateOne, it works; 2 times successively, it works. But if I call 2+ times in succession, only the first two ones correctly inserted into database, and the rest, no.
The error that I get after updateOne is, MongoWriteConcernError: No write concern mode named 'majority;' found in replica set configuration. However, I always get this error, also even when the insertion is done correctly. So I don't think this is related to my problem.
Probably you will suggest to me to use updateMany, bulkWrite, etc. and you will be right, but I want to know the reason why after 2+ the insertion is not done.
Have in mind .updateOne() returns a Promise so it should be handled properly in order to avoid concurrency issues. More info about it here.
The error MongoWriteConcernError might be related to the connection string you are using. Check if there is any &w=majority and remove it as recommended here.

Options to mongoose connect

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/

Saving cdrs manually using avp_db_query in Opensips

Is there a way to record cdrs manually using avp_db_query in opensips. I am using ACC table to record cdrs and than running procedure to transfer data to another table. But this put a lot of overhead on my DB due to too many calls. So is there any way that I can put directly cdrs in my actual table using AVP_DB_QUERY, I am doing for missing and not accepted calls but don't know how to do it for Answered calls.
You can program OpenSIPS to push CDR events over to the event interface instead of writing them to the database with:
do_accounting("evi", "cdr|failed");
Next, using the event_route module, you may subscribe to the E_ACC_CDR event by defining the route below, where you can perform your avp_db_query:
event_route [E_ACC_CDR]
{
fetch_event_params(...);
avp_db_query(...);
}

Sync elasticsearch on connection with database - nodeJS

Aim: sync elasticsearch with postgres database
Why: sometimes newtwork or cluster/server break so future updates should be recorded
This article https://qafoo.com/blog/086_how_to_synchronize_a_database_with_elastic_search.html suggests that I should create a separate table updates that will sync elasticsearch's id, allowing to select new data (from database) since the last record (in elasticsearch). So I thought what if I could record elasticsearch's failure and successful connection: if client ponged back successfully (returned a promise), I could launch a function to sync records with my database.
Here's my elasticConnect.js
import elasticsearch from 'elasticsearch'
import syncProcess from './sync'
const client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
client.ping({
requestTimeout: Infinity,
hello: "elasticsearch!"
})
.then(() => syncProcess) // successful connection
.catch(err => console.error(err))
export default client
This way, I don't even need to worry about running cron job (if question 1 is correct), since I know that cluster is running.
Questions
Will syncProcess run before export default client? I don't want any requests coming in while syncing...
syncProcess should run only once (since it's cached/not exported), no matter how many times I import elasticConnect.js. Correct?
Is there any advantages using the method with updates table, instead of just selecting data from parent/source table?
The articles' comments say "don't use timestamp to compare new data!".Ehhh... why? It should be ok since database is blocking, right?
For 1: As it is you have not warranty that syncProcess will have run by the time the client is exported. Instead you should do something like in this answer and export a promise instead.
For 2: With the solution I linked to in the above question, this would be taken care of.
For 3: An updates table would also catch record deletions, while simply selecting from the DB would not, since you don't know which records have disappeared.
For 4: The second comment after the article you linked to provides the answer (hint: timestamps are not strictly monotonic).

Collection name in Mongoose

Why would a database named 'blog' not allow a record insert and also give no return error and why would a database named 'blogs' allow a record inserts and return errors?
I just spent several hours going through all my code thinking I did something wrong. I have written many mongoose connected apps but when using the following it would return success but not insert the record and return no error as to why:
mongooose.connect('mongodb://localhost:27017/blog');
After banging my head against a wall for a bit I decided to change the database name:
mongooose.connect('mongodb://localhost:27017/blogs');
It works! But why would this name convention matter? I can't find anything in the documentation for MongoDB or Mongoosejs.
So I'm fairly certain mongodb doesn't care about database name "blog" vs "blogs". However, do note that mongoose has the questionably-helpful feature of silently queueing up operations while the database connection is still not established and then firing them off if/when the database connection is ready. That could be causing your confusion. To test that theory, pass a callback to mongoose.connect and put a console.log in the callback so you know exactly when the connection is ready.

Resources