Getting Mongo write error in Meteor - mongodb

I am trying to insert a new field like this, and I am getting a write error.
If I remove this line, the program works fine.
UserDetails.update({userId: Meteor.UserId()}, {$inc: {score: 5}});
Error trace is:
I20141107-12:55:38.278(5.5)? Exception in Mongo write: TypeError: object is not a function
I20141107-12:55:38.323(5.5)? at packages/mongo/mongo_driver.js:293
I20141107-12:55:38.323(5.5)? at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108)

I believe you just have a typo. It should be Meteor.userId()
Reference: https://docs.meteor.com/#/basic/Meteor-userId
UPDATE (11/9/14): It just dawned on me that you're using Meteor.userId() which gets the current user id from the Meteor.users collection. But it looks like you're trying to update the score for a user in a collection called UserDetails. The syntax to update a specific user id is this:
UserDetails.update("biwyMQCriR3KDFHod", {$inc: {score: 5} });
Where "biwyMQCriR3KDFHod" (with the double quotes) is the unique id value for that user.
I'm not sure how you're doing your update (perhaps you could share your code using http://meteorpad.com), but you might want to take a look at using Session.
Session References:
http://meteortips.com/book/sessions/
https://docs.meteor.com/#/basic/session

Related

MongoDB update deep nested field in the document

I try to update a field in the MongoDb like this
https://mongoplayground.net/p/_jFYJGi8z46
However i get this error
fail to run update: write exception: write errors: [The array filter for identifier 'e2' was not used in the update { $set: { sizes.$[e1].wares$[e2].reserved: "2" } }]
I can't find a bug :( ... it must be a very simple one
Right... one dot was missing :( This is solved
https://mongoplayground.net/p/obdFTr6G1kj

How do I create an array for an updateOne() call in mongoc (C libarary for Mongodb)?

I am completely mystified (and supremely frustrated). How do I create this call using the mongoc library?
I have the following doc structure in the collection
{_id: myOID,
subsriptions: {
newProducts: true,
newBlogPosts: true,
pressReleases: true,
}
}
I want to remove one of the subscriptions, for example, the user no longer wants to receive press releases from me.
This works in the mongo shell. Now I need to do it in C code
updateOne({_id: myOID}, [{'$unset': 'subscriptions.pressReleases'}], {})
Note how the update parameter in the Mongo shell is an anonymous array. I need to do that for the bson passed in as the update parameter in the mongoc_collection_update_one() API call.
The C code for updateOne is
mongo_status = mongoc_collection_update_one (mongo_collection,
mongo_query,
mongo_update,
NULL, /* No Opts to pass in */
NULL, /* no reply wanted */
&mongo_error);
Also note that in the aggregate() API, this is done with
{"pipeline" : [{'$unset': 'elists.lunch' }] }
Neither the updateOne() shell function nor the mongoc_collection_update_one() API call accept that, they want just the array.
How do I create the bson to use as the second parameter for mongoc_collection_update_one() API call?
Joe's answer works and I am able to accomplish what I need to do.
The $unset update operator takes an object, just like $set.
updateOne({_id: myOID},{'$unset':{'subscriptions.pressReleases': true}})
OR perhaps even better
updateOne({_id: myOID},{'$unset':{'subscriptions.pressReleases': {'$exists': true}}})
which will remove the subscription flag no matter what the value is for that field.
Doing it this way does not require an anonymous array (which I still don't know how to create).

Meteor update Mongo Doc by ID

I'm trying to update a Mongo record via Chrome's console.
Posts.update('hexidhere', {$set: {title: 'something text here'}});
The problem is with docs that were created a Mongo terminal. They were assigned an id like so (_str and _proto are nested inside the _id):
_id: LocalCollection._ObjectID
_str: '54ff06801ad15adbb3d1090'
_proto: LocalCollection._ObjectId
title: 'dummy title here'
When I added another test doc via chrome's console (not a mongo terminal) It seems to have added an ID correctly, and everything works as expected:
_id: 'EtPt9ntXtxG4qo9Tb'
title: 'dummy title here'
My question is:
Does anyone know a way to make the ID always be simple HexStrings (like in the second example), or is there a correct method for accessing the nested str value in the LocalCollection (I've tried Mongo.ObjectID('hexidhere'), 'theidhere', and a whole bunch of other stuff)?
Mongo likes to use ObjectId for _id, Meteor opted to use String. To learn more see the now deprecated google groups convo: https://groups.google.com/forum/#!topic/meteor-talk/f-ljBdZOwPk
To get the string of an ObjectId use the str method as in ObjectId("310458asdf323452").str See here for more info: http://docs.mongodb.org/manual/reference/object-id/

Flask-PyMongo collMod

I'm trying to update a TTL collection with the PyMongo. Trying to run this I get 'failed no such cmd: index'
client.db.command({'collMod': url,
'index': {'keyPattern': {'dateCreated':1},
'expireAfterSeconds': 3600}})
anyone shine some light on what I'm doing wrong?
I believe that this would work assuming that url contains the name of the collection with the index you are modifying:
client.db.command('collMod', url,
index={'keyPattern': {'dateCreated':1},
'expireAfterSeconds': 3600}})
For anyone else looking for a solution to this I managed with the following:
client.db.command('collMod', 'notifications',
index={'keyPattern': {'expr': 1},
'background': True,
'expireAfterSeconds': 604800})
Which results in the following output:
{u'expireAfterSeconds_old': 3888000,
u'expireAfterSeconds_new': 604800, u'ok': 1.0}
Well I've just decided to switch my index around to instead set the time I want the document to expire at in the database instead of saying how long I want the document to live by using
db.test.ensure_index("expireOn", expireAfterSeconds=0)
Now I can set the field to a future time that I want the entry to expire instead of having to change the index. Seems strange that as of now there is no way to update the index programatically using python.

Verify that I am connected to my MongoDB via Mongoose?

I am trying to learn Node.js and using MongoDB.
I got an insert working correctly and can insert as many objects as I want, however I cannot seem to query them at all.
I have tried using every technique posted here and none of them return any of my objects.
I have verified via the Mongo console that the objects exist but I just can't query them and I am absolutely lost as to why.
Here is the current code I'm using to query:
User.findOne({ 'user.name': 'James' }, function(user){
console.log("Got " + user);
res.send(user);
});
Help?
EDIT
The above code returns "null".
Nearly every time I post a question on SO lately I seem to find the answer myself within 15 minutes.
The answer to this one, is that my callback function is only accepting 1 argument of "user". The first argument in the callback is any Errors that are raised, so obviously there are no errors raised.
Changing the callback to this fixes it:
function(err, user) {
}