First time working with MongoDB. This query works for me in cmd line:
db.secrets.update({_id: ObjectId("5f767cd481cea1687b3dbf86")}, {$set: {secret_rating: 5}})
However, updating a record using essentially the same query on my node server is not completing this task when pinged. Am I wrong in trying to query for a record like so in my model? ObjectId obviously isn't native to my server.
db.secrets.update({_id: "5f767cd481cea1687b3dbf86"}, {$set: {secret_rating: 5}})
Assuming you're using the Nodejs Mongodb driver and not some ORM (since it hasn't been specified), two points of concern:
db
variable, you cannot reference collections directly such as you've done withdb.secrets
; you must instead use thecollection
method like so:So, unless you've assigned
db.secrets
withdb.collection("secrets")
you should be getting an error,Cannot read property "update" of undefined
. But I'm going to assume you've got the collection object indb.secrets
since you did not mention you're getting that error.ObjectID
object. You can import the ObjectID constructor from the nodejs driver like so:Then in your query, you will have to make a new ObjectID to get the correct result:
NOTE: The
ObjectID
constructor will throw an error if the string supplied to it is not a valid, 24-char hex string, so, if you're getting theid
string as an input from somewhere (say, as a parameter in an API or as a command line argument), you might want to wrap it in a function that handles that error.