lately I've been trying to update a price field in my mongodb database but I'm facing an annoying issue.
The collection I'm trying to modify is listingsandReviews, within the database sample_airbnb.
The field in question is made as such price {$numberDecimal: priceVar}.
On my back end (using Express.js DAO architecture), my update query looks as such:
// console.log(roomContent.amenities)
let price = roomContent.price.$numberDecimal
try {
const roomIdResponse = await rooms.updateOne(
{ _id: roomId },
{ $set: {
name: roomContent.name,
address: {street: roomContent.address.street},
price: {'$numberDecimal': price},
property_type: roomContent.property_type,
bedrooms: roomContent.bedrooms,
beds: roomContent.beds,
minimum_nights: roomContent.minimum_nights,
maximum_nights: roomContent.maximum_nights,
amenities: roomContent.amenities,
description: roomContent.description
}
},
{ upsert: true }
);
return roomIdResponse;
}
catch (e) {
console.error(`Unable to update room: ${e}`);
return { error: e };
}
}
the shell gives me back that error Unable to update room: MongoServerError: The dollar ($) prefixed field '$numberDecimal' in 'price.$numberDecimal' is not valid for storage.
I've been looking at every resource possible, some saying I should try NumberDecimal(), others decimal128, but nothing works so far.
You could say I could fix the issue on the front-end, but that wouldn't make the database structure consistent, which I want to avoid.