How To Update Many Mongo Documents Node.js async? - javascript

I have the async function below. DeviceStatus is a Mongo Collection of devices which has the property SerialNumber defined. Here, I'm collecting an array of of all devices in the DeviceStatus Collection and then iterating through each of them. I'd like to add a new property called NumberOfRetries and set it to zero. The function below works for the first DeviceStatus document. That is, in the loop it updates the document and adds the NumberOfRetries for the first document but does not complete the update for the remaining documents. Does anyone know what changes are required here so that I'm able to update all Mongo documents in the DeviceStatus collection.
static async addNumberOfRetriesCap(){
const devices = await DeviceStatus.find().exec();
for(let i = 0; i < devices.length; i++){
let device = devices[i];
await DeviceStatus.updateOne({"SerialNumber": device.SerialNumber}, {$set: {"NumberOfRetries" : "0"}})
}
}

you can use the $in operator and updateMany functionality.
const addNumberOfRetriesCap = async() => {
const devices = await DeviceStatus.find({}, {
SerialNumber: true
}).exec();
let deviceSerialNumber = []
for (let i = 0; i < devices.length; i++) deviceSerialNumber.push(devices[i].SerialNumber)
await DeviceStatus.updateMany({
"SerialNumber": {
$in: deviceSerialNumber
}
}, {
$set: {
"NumberOfRetries": "0"
}
})
}
If you want to update all values of the collection DeviceStatus you can direclty update the db like in the following example
const addNumberOfRetriesCap = async() => {
await DeviceStatus.updateMany({}, {
$set: {
"NumberOfRetries": "0"
}
})
}

Related

Can't update firebase collection field - Expected type 'ya', but it was: a custom Ia object

I am trying to make barbershop web app where costumer can see list of free appointments and when they reserve free appointment I want to delete that field from firebase.
I have a collection which represents one barber.
This is how it looks in firebase.
As you see radno_vrijeme is object or map in firebase which contains 6 arrays, and in each array there is list of free working hours.
In my function I am able to do everthing except last line where I need to update firebase collection.
const finishReservation = async () => {
try {
const freeTimeRef = collection(db, `${barber}`);
const q = query(freeTimeRef);
const querySnap = await getDoc(q);
querySnap.forEach(async (doc) => {
const radnoVrijeme = doc.data().radno_vrijeme;
// Find the index of the hour you want to delete
const index = radnoVrijeme["Mon"].indexOf(hour);
// Remove the hour from the array
radnoVrijeme["Mon"].splice(index, 1);
// Update the document in the collection
console.log(radnoVrijeme);
const radnoVrijemeMap = new Map(Object.entries(radnoVrijeme));
await freeTimeRef.update({ radno_vrijeme: radnoVrijemeMap });
});
} catch (error) {
console.log(error);
}
};
I tried to pass it as JSON stringified object, but it didn't work. I always get this error :
"FirebaseError: Expected type 'ya', but it was: a custom Ia object"
When you are trying to fetch multiple documents using a collection reference or query, then you must use getDocs():
const finishReservation = async () => {
try {
const freeTimeRef = collection(db, `${barber}`);
const q = query(freeTimeRef);
const querySnap = await getDocs(q);
const updates = [];
querySnap.forEach((d) => {
const radnoVrijeme = d.data().radno_vrijeme;
const index = radnoVrijeme["Mon"].indexOf(hour);
radnoVrijeme["Mon"].splice(index, 1);
const radnoVrijemeMap = new Map(Object.entries(radnoVrijeme));
updates.push(updateDoc(d.ref, { radno_vrijeme: radnoVrijemeMap }))
});
await Promise.all(updates);
console.log("Documents updated")
} catch (error) {
console.log(error);
}
};
getDoc() is used to fetch a single document using a document reference.

Loop to create or update nodes in Neo4j

I am new to Neo4j so I quite stuck with looping through some values.
I have a list of skill to skill strings
let data = [
'big_data, business_intelligence',
'big_data, data_collection',
'big_data, economic_growth',
'big_data, economy'
]
And I want to create or update the relation between left side with right side
for (let item of data) {
CreateSkillToSkillRelation(item);
}
const CreateSkillToSkillRelation = async (relation) => {
let mainSkill = relation.split(",")[0];
let secundarySkill = relation.split(",")[1];
try {
// Check if the relationship exists
let { records } = await session.run(
"MATCH(main:SKILL {name:$mainSkill}) -[relation:SKILL_TO_SKILL]-> (secundary:SKILL {name:$secundarySkill}) RETURN relation",
{ mainSkill, secundarySkill }
);
let count =
records[0]?._fields[0].properties.count > 0
? records[0]._fields[0].properties.count + 1
: 1;
// If count is greater then 1 then lets update the counter
if (count > 1) {
await session.run(
"MATCH(main:SKILL {name:$mainSkill}) -[relation:SKILL_TO_SKILL]-> (secundary:SKILL {name:$secundarySkill}) SET relation.count = $count RETURN main, secundary",
{
mainSkill,
secundarySkill,
count,
}
);
}
// Otherwise the skill relation is not created so lets create one
else {
await session.run(
"CREATE(main:SKILL {name:$mainSkill}) -[:SKILL_TO_SKILL {count:$count}]-> (secundary:SKILL {name:$secundarySkill}) RETURN main, secundary",
{
mainSkill,
secundarySkill,
count,
}
);
}
await session.close();
} catch (error) {
console.log(error);
}
};
But every time when I run this I get the following error Neo4jError: Queries cannot be run directly on a session with an open transaction; either run from within the transaction or use a different session.
Any idea how can I solve this?
for (let item of data) {
CreateSkillToSkillRelation(item);
}
Is not awaiting the promises you create and so you are basically trying to run all of these promises concurrently against a single session which only supports a single concurrent transaction.
You should create a session in each call of CreateSkillToSkillRelation or await each call to it using a single session.
Though note you close the session at the end of CreateSkillToSkillRelation but only on success, might I suggest moving await session.close(); into a finally block.
The answer of the colleague #just_another_dotnet_dev is absolutely correct: you run asynchronous functions in a loop, and close the session in one of them.
The Cipher language is very rich, and you can use it to do everything that you tried to do with a loop in Javascript. Something like this, using UNWIND and MERGE:
const CreateSkillToSkillRelations = async (data) => {
const session = driver.session();
try {
let { records } = await session.run(
`WITH split(row, ',') as rels
WITH trim(rels[0]) as mainSkill,
trim(rels[1]) as secundarySkill
MERGE (main:SKILL {name: mainSkill})
-[relation:SKILL_TO_SKILL]->
(secundary:SKILL {name: secundarySkill})
ON CREATE SET relation.count = 1
ON MATCH SET relation.count = relation.count + 1
RETURN main, relation, secundary`,
{ data }
);
} catch (error) {
console.log(error);
} finally {
await session.close()
}
};

Updating multiple objects in an Array belonging to a collection

I'm using MERN stack for my program with mongoose for accessing the database. I have a collection called Movies and I wanted to edit multiple objects in an array within this collection. This is what the Movie Schema contains in my database:
I wanted to edit multiple objects in the 2D array within seats and to change isReserved to True.
I just used findOne in accessing the data since I still don't know how to update the objects that I want to access.
app.post('/confirm/:movieId/:timeId', (req, res) => {
const movieId = req.params.movieId;
const timeId = req.params.timeId;
const selectedSeats = req.body;
// console.log("in confirm DB ");
// console.log(selectedSeats);
let getSeats;
let getTimeSlots;
const length_timeId = timeId.length;
Movies.findOne({ movieId }, (err, movie) => {
console.log("INSIDE");
getTimeSlots = movie['timeslots'];
let index = timeId.substring(1, length_timeId);
//get the seats
getSeats = getTimeSlots[parseInt(index)-1];
//loop through seats
console.log("PRINTING GET SEATS");
console.log(getSeats);
for(var i=0; i<selectedSeats.length; i++) {
let row = parseInt(selectedSeats[i] / 5);
let id = selectedSeats[i] % 5;
console.log(getSeats["seats"][row][id]);
}
})
})
I already accessed the objects that I want to edit as that code displays this on my terminal:
Would really appreciate some tips on how to update the isReserved status. Thanks!
Do you specify the timeslot and seat by an id or by the index within the array? If you use the index then solution is quite simple
const key = "timeslots." + req.params.timeId + ".seats." + req.body + ".isReserved";
var upd = {};
upd[key] = true;
db.Movies.updateOne({ movieId: req.params.movieId }, { $set: upd })
If your code uses the id then you have to work with array filters, see Update Nested Arrays in Conjunction with $[], so similar to this
db.Movies.updateOne(
{ movieId: req.params.movieId },
{ $set: { "timeslots.$[slot].seats.$[seat].isReserved": true } },
{ arrayFilters: [ { "slot.id": req.params.timeId } , { "seat.id": req.body } ] }
)

Pushing mongoDB results to an array, but the array remains empty

This is my first question on stack overflow, so bear with me.
So I have a controller function that is rendering my google maps api, and I am trying to loop through the results from mongoDB and push them to an array so that I can pass it to a script tag on the ejs. I am able to console.log(myArr) within the for loop and get results, but not outside of it (just above the res.render). I am assuming my that my problem is my result within res.render is receiving an empty array.
Please help me I have been stuck on this problem for days now. Thank you, Andrew
function showMap(req, res) {
let myArr = [];
db.collection("poppies").find().toArray(function (err, result) {
for (let i = 0; i < result.length; i++) {
myArr.push(result[i].Address);
};
});
res.render('map', {
result: JSON.stringify(myArr)
})
};
Asynchronous Javascript allows you to execute operations without waiting for the processing thread to become free.
Imagine the data loads from your DB only after 3 seconds - you should "wait" to get the value from your DB before running the next line code.
In your case you use myArr "outside" without await -Or- promises -Or- callbacks) - So the value is an empty array.
IMPORTANT: The idea of Asynchronous Javascript is a topic for a course
(No way to cover this issue by StackOverflow Answer).
Not working
length is: undefined
function helloWorld() {
let items = db.collection("newspapers").find({}).toArray();
return (items);
};
const result = helloWorld();
console.log("length is: " + result.length); /* length is: undefined */
Working
await for the value from helloWorld() function.
function helloWorld() {
let items = db.collection("newspapers").find({}).toArray();
return (items);
};
const result = await helloWorld();
console.log("length is: " + result.length); /* length is: 337 */
In practice
Promise chaining for example:
For this database:
[
{
"Newspaper": "The New York Times"
},
{
"Newspaper": "Washington Post"
}
]
const dbName = 'my_database';
await client.connect();
const db = client.db(dbName);
myArr = [];
db.collection("newspapers").find({}).toArray()
.then(
res => {
for (let i = 0; i < res.length; i++) {
myArr.push(res[i].Newspaper);
};
console.log(`The length is ${res.length} documents`) /* The length is 2 documents */
console.log(myArr); /* [ 'The New York Times', 'Washington Post' ] */
},
err => console.error(`Something went wrong: ${err}`),
);
try/catch Async/await example (Readable code pattern):
const helloWorld = (async () => {
try {
let items = await db.collection("newspapers").find({}).limit(2).toArray();
return (items);
} catch (e) {
console.error(
`Unable to establish a collection: ${e}`,
)
}
})
const result = await helloWorld();
console.log("length is: " + result.length); /* length is: 2 */
More examples here:
https://docs.mongodb.com/drivers/node/fundamentals/promises

firestore query error for subcollections: rest.collection is not a function

I want to count the number of branches of all restaurants. "Branch" is a sub collection of "Restaurants". When I try to do this query, there is an error:
rest.collection is not a function
Here's my code. How can I fix it?
async function getBranch(){
const size = 0;
const restRef = await firebase.firestore().collection('Restaurants').get();
restRef.forEach((rest) => {
const branchRef = rest.collection('Branch').get();
size = size + branchRef.size();
})
return size;
}
you have to provide id of restaurants to get the subcollection. So better make reference for Restaurants and get all branches
async function getBranch(){
const size = 0;
const restRef = firebase.firestore().collection('Restaurants');
const restRes = await restRef.get();
restRef.forEach((rest) => {
const branchRef = await restRef.doc(rest.id).collection('Branch').get();
size = size + branchRef.size();
})
return size;
}
You could do as follow, by using Promise.all() (untested).
async function getBranch(){
let size = 0;
const restQS = await firebase.firestore().collection('Restaurants').get();
const promises = [];
restQS.forEach((rest) => {
promises.push(rest.collection('Branch').get());
});
const querySnapshotsArray = await Promise.all(promises);
querySnapshotsArray.forEach(qs => {
size += qs.size; // <= Note that size is a property of the QuerySnapshot, not a method
})
return size;
}
Another approach would be to query all your branch doc with a Collection group query, if the only collections named Branch are subcollection of Restaurants.
const branchQS = await firebase.firestore().collectionGroup('Branch').get();
return branchQS.size;
HOWEVER,
you should note that this implies that you read ALL the documents of ALL the (sub)collections each time you want to get the number of branch documents and, therefore, it has a cost.
So, if your collections have a lot of documents, a more affordable approach would be to maintain a set of distributed counters that hold the number of documents. Each time you add/remove a document, you increase/decrease the counters.
See here in the doc for more details, as well as this answer.

Categories