Reputation: 953
I would like to use a $ in a Mongoose Schema (data is formatted this way from another app), like this:
const mySchema = new mongoose.Schema({
$meta1: {
type: String,
trim: true,
},
$meta2: {
type: String,
trim: true,
},
normalKey3: {
type: String,
trim: true,
}
});
When I attempt this, and run the function (with PUT route):
exports.updateMe = async (req, res) => {
const xyz = await MySchema.findOneAndUpdate({normalKey3: req.body.normalKey3}, req.body,
{
upsert: true,
new: true,
runValidators: true
}).exec();
res.status(200).json(xyz);
};
I get the error below. If I replace the $ with a _ (e.g. _meta1) it works. Is this a Mongoose or Mongo issue? What does $ signify such that it doesn't work?
Error:
Error: Invalid atomic update value for $meta1. Expected an object, received string
[💻] at castUpdate (/Users/.../node_modules/mongoose/lib/services/query/castUpdate.js:86:13)
[💻] at model.Query._castUpdate (/Users/.../node_modules/mongoose/lib/query.js:3185:10)
[💻] at castDoc (/Users/.../node_modules/mongoose/lib/query.js:3212:18)
[💻] at model.Query.Query._findAndModify (/Users/.../node_modules/mongoose/lib/query.js:2441:19)
[💻] at model.Query.Query._findOneAndUpdate (/Users/.../node_modules/mongoose/lib/query.js:2163:8)
[💻] at process.nextTick (/Users/.../node_modules/kareem/index.js:315:33)
[💻] at process._tickCallback (internal/process/next_tick.js:61:11)
Upvotes: 2
Views: 779
Reputation: 144
$ is reserved keywords in MongoDB there are limits in MongoDB names
Restrictions on Field Names Field names cannot contain the null character. Top-level field names cannot start with the dollar sign ($) character. Otherwise, starting in MongoDB 3.6, the server permits storage of field names that contain dots (i.e. .) and dollar signs (i.e. $).
you can find more information here Visit https://docs.mongodb.com/manual/reference/limits/
Upvotes: 3