Meteor update Mongo Doc by ID - mongodb

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/

Related

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.

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

Mongo find by regex: return only matching string

My application has the following stack:
Sinatra on Ruby -> MongoMapper -> MongoDB
The application puts several entries in the database. In order to crosslink to other pages, I've added some sort of syntax. e.g.:
Coffee is a black, caffeinated liquid made from beans. {Tea} is made from leaves. Both drinks are sometimes enjoyed with {milk}
In this example {Tea} will link to another DB entry about tea.
I'm trying to query my mongoDB about all 'linked terms'. Usually in ruby I would do something like this: /{([a-zA-Z0-9])+}/ where the () will return a matched string. In mongo however I get the whole record.
How can I get mongo to return me only the matched parts of the record I'm looking for. So for the example above it would return:
["Tea", "milk"]
I'm trying to avoid pulling the entire record into Ruby and processing them there
I don't know if I understand.
db.yourColl.aggregate([
{
$match:{"yourKey":{$regex:'[a-zA-Z0-9]', "$options" : "i"}}
},
{
$group:{
_id:null,
tot:{$push:"$yourKey"}
}
}])
If you don't want to have duplicate in totuse $addToSet
The way I solved this problem is using the string aggregation commands to extract the StartingIndexCP, ending indexCP and substrCP commands to extract the string I wanted. Since you could have multiple of these {} you need to have a projection to identify these CP indices in one shot and have another projection to extract the words you need. Hope this helps.

Autocomplete function not working in meteor

I have an autocomplete textbox function which I am using in meteor.
It works fine for the following hardcoded data like :
$(document).ready(function() {
$("#demo-input-facebook-theme").tokenInput(
[{id: 7, name: "Ruby"},{id: 11, name: "Python"},{id: 13, name: "JavaScript"}],
{theme: "facebook"}
);
});
Now , I had fetched data from database MongoDB, when I pass this data as parameter to the autocomplete function it does not work... I have also used the method JSON.stringify().
The returned data looks like :
[{"_id":"ab170916-a44b-49f9-85ef-a34c90fb815d","Namelist_name":"Badminton"},
{"_id":"f768e4ba-b628-4d3f-8da6-0bad31346dcc","Namelist_name":"Biking"},
{"_id":"0bee086b-1785-40c9-9c5d-a39331c875e1","Namelist_name":"Chess"},
{"_id":"4eae1e54-ec60-4578-8052-0bf1bccf13b1","Namelist_name":"Golf"},
{"_id":"a0d2b89e-a2d6-4b30-8e38-779c5a886d49","Namelist_name":"Hiking"},
{"_id":"f3a05456-38d4-40f2-86b1-eddea061fdf0","Namelist_name":"Tennis"},
{"_id":"3669b9a2-3f87-4579-8064-82d627196fcb","Namelist_name":"Walking"},
{"_id":"6ac6497e-82b2-40fe-8b24-152e9f42750d","Namelist_name":"Wine Tasting"},
{"_id":"15a7ca87-aef7-43ab-945b-168647bb59aa","Namelist_name":"Yoga"},
{"_id":"bc40d166-64ef-4e61-85cd-60064dc037cd","Namelist_name":"Zumba"}]
Just change the Namelist_name with only name keyword. Since jquery tokeninput uses name as key
as mentioned in your hardcoded data. And if we download jquery tokeninput from http://loopj.com/jquery-tokeninput/. we come to know that the keyword is 'name'.
Hope this helps....
If you compare your returned JSON data with the test data that works there is one essential difference: you are missing the id field in your MongoDB JSON results and instead providing _id. The id field is currently a hardcoded default for the jQuery tokenInput plugin you are using for autocomplete.
Several folks have submitted patches to allow setting a different key using the tokenValue parameter.
Example (untested) patch: tokenValue cannot be changed.
If you're autocompleting multiple items with free text, you may want to check out this package I created:
https://github.com/mizzao/meteor-autocomplete

Finding subfields in mongoDB

How would I select the subdocument author, in the below shown object, without directly accessing it, ie authors[0], so that the result that I get back is {"_id":"4f44af6a024342300e000002", visible: true}?
The object:
{
_id: "4f44af6a024342300e000001",
title: "A book",
created: "2012-02-22T14:12:51.305Z"
authors: [{"_id":"4f44af6a024342300e000002", visible: true}]
}
I have been able to update it by doing this, but selecting it seems like a completely different story:
books.update({_id: "4f44af6a024342300e000001",'authors._id': "4f44af6a024342300e000002"},
{$set: {'books.$.visible': true}}, function(err) {
// ...
});
It is currently not possible (see a similar question with extra details)
In MongoDB, how does on get the value in a field for an embedded document, but query based on a different value
In the next version, the aggreation framework could give some step in this direction, but the real solution are the virtual collection.
For the time being (in short), use an embedded document only if you go through the parent.