Flask-PyMongo collMod - mongodb

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.

Related

Prevent rounding errors when using $inc in mongodb on US currency?

I am calling update from node to add money to a user's account. Starting from 0, these two sequential updates:
83.94 and then 546.13 result in 630.0699999999999.
This is the same result as if you do this in JS
83.94+566.13
This is the mongo call:
xxx.update({_id: xId},
{
$inc: {money: dollars}
});
Is there any way to prevent this and get reasonable behavior? (besides converting to pennies instead of dollars).
Do I really have to "round to 2" every time I read this field?
Thanks #ryan. Just what I needed.
https://docs.mongodb.com/manual/tutorial/model-monetary-data/
https://docs.mongodb.com/manual/core/shell-types/#shell-type-decimal

Elasticsearch mongodb river script in index doesn't work

I'm trying to change few fields strings using javascript.
For example take only the last part of the URL taken from mongo through the river so in elasticsearch I'll have only the end of it.
When creating the index (using curl) I added under "options" the following script:
"script": "ctx.document.shorturl = ctx.document.url.substr(-4);delete ctx.document.url;
I tried some manipulations such as adding \"...\" or use ctx['doc']['url'] and others but nothing seems to work.
I always get only url field with the full url (shorturl is not created at all).
Can anyone suggest what is the right syntax to make it work?
Another thing I need to do is combine to fields - lat & long, to one "location" field in order to use it in Kibana, can anyone suggest the right script for that? (create new field called "location" which contain both field "lat" & "long" with comma between them).
Thanks.
You did substring(-4), hence it will return the whole string. You should use substring(4) instead:
ctx.document.shorturl = ctx.document.url.substr(4);delete ctx.document.url;

Getting Mongo write error in Meteor

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

How to get whole index in ZEND Lucene?

Hi I am looking for a way to get the whole index if my query is nothing.
My lucene is a picture of my database without some unwanted content, like expired annonces...
So I would like to use only lucene to get annonces, and for that I need a way to get the whole index.
Any ideas?
thanks!
This is not the answer but something wich works for me:
Using an indexKey like is_indexed, always true.
I am adding "is_indexed:1" to my query and it works...
If you have something else, let me know!
I tend to use a field which is named after the index such as:
$oDoc->addField(
Zend_Search_Lucene_Field::keyword(
'index',
'availability'
)
);
Then the term query will return all fields. It's not pretty but it works fine.

MongoDB update query is failing silently

I have an app using Mongoid on top of MongoDB and an update is failing silently.
The code looks like this:
# Are we getting a new attribute? Yes we are!
p "attr first name = #{#attributes['first_name']}"
if user.update_attributes!(#attributes)
u = User.find(params[:id]).to_json
end
No exception is thrown in this code. So I looked at my MongoDB log and constructed this query based on what mongo is trying to do:
db.users.update({ "_id": "4d5561276ce886c496000001" }, { $set: { "first_name": "Erinamodobo" } }, false);
Now this does not cause any exceptions but when I grab the record that was supposed to be updated with this query:
db.users.find({"email":"escharling#somecompany.com"})
I see that the "first_name" attribute has not been updated.
Any idea why this could be happening? Sounds like something stupid.
Thanks!
You need to find out what
user.update_attributes!(#attributes)
is actually doing. This could be an issue with Mongoid. When you configure Mongoid, you can set up a logger. There you should be able to see what the driver is writing to MongoDB, and that should help answer your question. The next best thing is to post your code to the Mongoid mailing list (http://groups.google.com/group/mongoid) where people who work with Mongoid all the time will probably know what's going on.
Updating Mongoid to the latest rc.7 fixed this issue
Enable "safe mode" on your operations.