I have a document that looks like:
{
"personName": "Some name",
"metaDetails": {
"visits": 1,
"otherMeta": 32
}
}
Using Mongoose for Node.js. I’d like to update (or insert) the visits count. Here’s what I have so far. I’ve read about the $ positional operator but I might be way off:
updObj = {}
newKeyString = "metaDetails.$.visits"
updObj[newKeyString] = 1
Person.update {personName: "Some name}, {$inc: updObj}, {upsert: true}, (err, updRes) ->
This doesn’t appear to actually update or insert any documents however. Any help?
EDIT: Added the model
mongoose = require 'mongoose'
PersonSchema = new mongoose.Schema
personName:
type: String, index: true, unique: true
metaDetails: []
This is my model. metaDetails is intentionally not defined beyond just an array because the data within can be highly variable
Why do you put the $ operator inside another object. In javascript it ll become a associative array, but $inc accepts the string equivalent of it.
Try this function