I am using MongoDB with Javascript. I am trying to declare the following parameters:
params = {
title: title,
description: description,
"media.image": image,
feedUpdated: new Date()
};
Then I run a the query using a Service that includes the use of $set as follows:
collection.update({
_id: ObjectId(id)
}, {
$set: {
params
}
}, function(e, updated) {
cb(e, updated);
});
But I get the following error:
MongoError: The dotted field 'media.image' in 'params.media.image' is not valid for storage.
I think the issue is due to the fact that by declaring the object using quotes it declares it as an object key "media.image" instead of leaving it as a string. According to MongoDB $set documentation:
To specify a in an embedded document or in an array, use dot notation.
https://docs.mongodb.com/manual/reference/operator/update/set/
I could technically write manually the query to include 'media.image' but I would prefer to declare it in the params and pass it to the Service.
Any ideas? All approaches are welcome!
$set: params. When you do$set: { params }it gets expanded as applying"params"as the name of the key for the object. Much the same as writing'$set': { 'params': params }, which of course you do not want.paramsis already an object. You don't need to wrap it in one.