node-mongodb show update results - node.js

When I db.collection('example').update({"a":1},{"$set":{"b":2}},{multi:true},function(e,r){
I get r:
{
n:3,
nModified:3,
ok:1
}
This works, I can see If I look at my db that I have successfully updated 3 documents but where are my results?
Quoted from https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html
callback is the callback to be run after the records are updated. Has three parameters, the first is an error object (if error occured), the second is the count of records that were modified, the third is an object with the status of the operation.
I've tried with 3 outputs in the callback but, then I just get null as a result
db.collection('example').update({"a":1},{"$set":{"b":2}},{multi:true},function(e,n,r){
My documents have been successfully updated but r is null!
I am expecting for this to return my updated documents
It doesn't look like this operation ever does, so how can I manullay return the documents that got changed?

You can use findAndModify to get the updated document in the result. It's callback has 2 parameters:
1- error
2- Updated document
I am not sure this would work for you, but check [documentation]: https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html#find-and-modify for more info.

To get the updated documents in the returned result, you'll need to use the db.collection.bulkWrite method instead.

Related

Sequelize: Force update for a JSON array

Sequelize won't update a JSON field under some circumstances.
For example, I have:
[[1]] (an array inside array)
And I'm trying to push something:
instance.arr[0].push(1); // [[1,1]]
instance.save();
// or:
instance.update({arr: instance.arr});
Now inside the instance I have changed the array and nothing changed inside the database. Not even a query is sent. :(
From the Sequelize website:
https://sequelize.org/master/manual/model-instances.html
The save method is optimized internally to only update fields that really
changed. This means that if you don't change anything and call save,
Sequelize will know that the save is superfluous and do nothing, i.e.,
no query will be generated (it will still return a Promise, but it
will resolve immediately).
That's good, but it seems like it doesn't work for JSON. Can I do a force update?
As of today, I have to do a deep copy of the array to save it.
I'm using MariaDB. I don't know if that matters.
It seems you have to specify that the field has changed
instance.changed( 'arr', true);
instance.save

How to get all mentions from a message

I made a function which needs 2 mentions, so I first check the size with:
if (message.mentions.members.size == 2)
It works, but then I need to get both mentions. The following works, but it only gets me the first one:
message.mentions.members.first()
I've also tried other ways to get both mentions, like this one below, that returns undefined.
message.mentions.members[0]
first() takes an optional count parameter if you see here.
You can use this to return an array of the members, like so:
message.members.first(2);
The reason mentions.members[0] doesn't work is because members is a Collection (a discord.js enhancement of a map) which uses a snowflake as a key, 0 is not a valid snowflake and thus returns undefined.

How to tell if a Firestore operation succeeded or failed?

I need to be able to tell if a set(), delete(), or update() operation on a document has succeeded before I do a transaction operation to increment or decrement a counter.
I've tried to print what set(), delete() and update() return, but it always just returns "seconds" and "nanos" whether or not the operation succeeded or not. I've tried to do operations on document IDs that don't exist, or collections that don't exist, but it always just returns the same thing with no indication if it did anything or not.
collection.("some_col").document("SoM3DoC").delete()
collection.("some_other_col").document("SoM30tH3RDoC").collection("some_col_ref").document("SoM3DoC").delete()
Then, ONLY if the above succeeded (the document existed and was deleted):
some_transaction(transaction, collection.("some_other_col").document("SoM30tH3RDoC")) # decrement a counter in this doc
I'm expecting that the operation methods would either throw an error if it couldn't complete the operation or return some message to indicate it but I can't seem to get any response. I even tried starting with some random collection like collection.("asdfsergreasg").document... but there's still no response.
The API documentation indicates what to expect from various operations, so you should use that as a reference. Some operations on documents and collections that don't exist don't yield errors. Some do. For example, calling get on a document that doesn't exist isn't an error, but the returned object will be clear that no document exists. However, calling update on a document that doesn't exist should raise an error.

Get the updated document after update_one

What parameter needs to pass for the MongoEngine update_one() to get the updated document in return? Currently, it is returning 0 or 1.
Instead of using update_one() I used the method modify(). Pass the parameter new = True to it to get the updated result
According to the mongoengine documentation there doesn't seem to be anything you can pass to update_one to return the updated document. I've tried this myself in a test environment and I'm also getting either a 0 or 1 returned.
There is a parameter you can pass to update called full_result which gives you a bit more information but it's nothing that would help you in this case.

couchDB conflicts when supplying own ID with large inserts using _bulk_docs

Same code works fine when letting couch auto generate UUID's. I am starting off with a new completely empty database yet I keep getting this
error: conflict
reason: Document update conflict
To reiterate I am posting new documents to an empty database so not sure how I can get update conflicts when nothing is being updated. Even stranger the conflicting documents still show up in the DB with only a single revision, but overall there are missing records.
I am trying to insert about 38,000 records with _bulk_docs in batches of 100. I am getting these records (100 at a time) from a RETS server, each record already has a unique ID that I want to use for the couchDB _id instead of their UUID's. I am using a promised based library to get the records and axios to insert them into couch. After getting the first batch of 100 I then run this code to add an _id to each of the 100 records before inserting
let batch = [];
batch = records.results.map((listing) => {
let temp = listing;
temp._id = listing.ListingKey;
return temp;
});
Then insert:
axios.post('http://127.0.0.1:5984/rets_store/_bulk_docs', { docs: batch })
This is all inside of a function that I call recursively.
I know this probably wont be enough to see the issue but thought Id start here. I know for sure it has something to do with my map() and adding the _id = ListingKey
Thanks!

Resources