As part of a school exercise and my personal project, I am working on setting up a NodeRed server that retrieves weather data, stores it in MongoDB, and retrieves it for future searches on the same location.
My NodeRed server is attempting to perform a replaceOne() operation on a remote MongoDB database using the "node-red-contrib-mongodb4" v 2.3.0 node. The goal is to verify if a document with matching values for city, country, and openweathermap_id exists. If it does, then replace it; otherwise, create a new document.
The issue arises when passing the object to the MongoDB node, resulting in a "MongoInvalidArgumentError: Document must be a valid JavaScript object" error. While insertOne() functions properly, replaceOne() seems to trigger this error.
For more information about the Node homepage, you can visit:
The connection configuration is not the source of the error, as other operations to the same MongoDB database work smoothly.
Despite trying various structures and methods for the object, the problem persists. Here is the most recent structure used:
The object (msg.payload) is passed to the function node.
{"city":"Happy Place","country":"Over There","weather":"Clouds","tempc":14.2,"temp_maxc":15,"temp_minc":14,"humidity":84,"pressure":1023,"windspeed":2.57,"winddirection":210,"clouds":40,"description":"The weather in Happy place at coordinates: 32.2125, 75.1209 is Clouds (scattered clouds).","openweathermap_id":111,"timestamp":"2023-08-24T15:06:09.219Z"}
Wrapping the object in an array was also attempted, which worked with insertOne() but failed with replaceOne().
Below is the code snippet from the function node leading to the MongoDB node:
msg.filter = {
city: {$eq: msg.payload.city},
country: {$eq: msg.payload.country},
openweathermap_id: { $eq: msg.payload.openweathermap_id }
};
msg.replacement = {
city: msg.payload.city,
country: msg.payload.country,
weather: msg.payload.weather,
tempc: msg.payload.tempc,
temp_maxc: msg.payload.temp_maxc,
temp_minc: msg.payload.temp_minc,
humidity: msg.payload.humidity,
pressure: msg.payload.pressure,
windspeed: msg.payload.windspeed,
winddirection: msg.payload.winddirection,
clouds: msg.payload.clouds,
description: msg.payload.description,
openweathermap_id: msg.payload.openweathermap_id,
timestamp: msg.payload.timestamp
};
msg.options = {
upsert: true
};
return msg;
An image of the MongoDB node settings can be viewed here.
The exact reason why the object is considered invalid in JavaScript is still unclear to me.