These are chat archives for Automattic/mongoose

16th
Apr 2015
Karl Brightman
@karlbright
Apr 16 2015 00:33
Has anyone had any issues with pre hooks firing with undefined on 'this'?
https://gist.github.com/anonymous/7b85bcc133a9a90fd218 Each hook here is returning undefined for 'this'.
and yes, i've fixed the issue on line 31 :)
Alex Hatzipanis
@hatzipanis
Apr 16 2015 01:41
If I had, say, four models and I wanted to grab the latest 8 documents across them all (not 8 for each model), how would I go about that? I realise I can't query multiple models at once, so I'm considering doing 4 queries for, say, 10 documents each, adding them to an array then sorting that and taking the first 8. Is there are better/more efficient way of doing this?
Karl Brightman
@karlbright
Apr 16 2015 02:45
In case anyone was interested, my issue was with the fat arrow being used. Oops.
@hatzipanis The only other way is to keep a collection of updates across the four models. Add a document each time you add one of the models.
Then query that for the last 8?
Valeri Karpov
@vkarpov15
Apr 16 2015 13:53
@karlbright I'd also be careful - pre('update') is query middleware rather than document middleware - in pre('update'), this is the query rather than the document, because the document being updated may not even exist in memory on the machine mongoose is running. You may instead want to do this.update({}, { $set: { created_at: now } });
@hatzipanis there's really no good way to do that in mongodb without doing 4 queries for 8 documents each. MongoDB can't sort across multiple collections. If you need to do this in your application and need it to be fast, I'd recommend migrating the 4 models into a single collection and putting the models behind a discriminator
feargal9
@feargal9
Apr 16 2015 17:01
Hi! I'm working on a task management application and have a task schema with title, description etc. I'm wondering if anyone can suggest a good approach for assigning a custom id to each task so on the front end the tasks can be found by searching for a user friendly task id.
Valeri Karpov
@vkarpov15
Apr 16 2015 18:06
depends on how you define user-friendly :)
tushar dhingra
@tushki
Apr 16 2015 18:07
@vkarpov15 hey please could you help me out with my problem
i am facing it since 2 weeks now
Valeri Karpov
@vkarpov15
Apr 16 2015 18:08
for a reasonably technical audience the ObjectID hex can be sufficient, if you want something a little more friendly you can always take appear.in's approach and pick 2-3 dictionary words
tushar dhingra
@tushki
Apr 16 2015 18:08
i want to export data from mongodb to elasticsearch
i used river plugin
and others too they use oplog which is not reliable so could you please suggest any other way out
???
Valeri Karpov
@vkarpov15
Apr 16 2015 18:09
@tushki the SO answer you posted has more detail than I am currently capable of providing. I don't have much experience with elasticsearch and don't have a spare weekend to learn it during the next few weeks.
tushar dhingra
@tushki
Apr 16 2015 18:09
@vkarpov15 hhahah cool
Valeri Karpov
@vkarpov15
Apr 16 2015 18:09
Wish I could be more helpful
tushar dhingra
@tushki
Apr 16 2015 18:09
could you help me with oplog
?
Valeri Karpov
@vkarpov15
Apr 16 2015 18:10
why is oplog not reliable?
tushar dhingra
@tushki
Apr 16 2015 18:10
which is called i mean how i can edit it and anything you have
it is stale it crashes everytime when mongodb is restarted
and all kind of errors i had faced withit
could you suggest me where i can find them
?
Valeri Karpov
@vkarpov15
Apr 16 2015 18:11
where you can find what exactly?
tushar dhingra
@tushki
Apr 16 2015 18:11
where i kind the answer
i mean any forum
you are aware of
?
anything for me to follow up
?
feargal9
@feargal9
Apr 16 2015 18:12
I suppose when I say user friendly I mean if there were a few thousand tasks on the site, and a user wanted to find a particular one, they could search for some custom id like '123'. This would also be helpful is a user is creating a task and wants to associate another task with the current one.
The only thing I know is similar is in team foundation server by microsoft, there are work item numbers and you can just enter a number, e.g. 123456 in the search bar and be brought to the work item/task you want to view
Valeri Karpov
@vkarpov15
Apr 16 2015 18:13
@feargal9 you could just use the _id ObjectID :)
@tushki in terms of using the mongodb oplog?
tushar dhingra
@tushki
Apr 16 2015 18:14
anything
Valeri Karpov
@vkarpov15
Apr 16 2015 18:14
tushar dhingra
@tushki
Apr 16 2015 18:14
ok cool thanks lemme check
Valeri Karpov
@vkarpov15
Apr 16 2015 18:15
you can also try this module: https://www.npmjs.com/package/mongo-oplog . Oplog does become stale if mongodb restarts, but that's why you run mongodb with 3 node replica set if you want automatic failover
when new primary is elected, you can start tailing from the new primary, which probably still has data in its oplog
unless all the mongods in your replica set crashed right around the same time
feargal9
@feargal9
Apr 16 2015 18:22
@vkarpov15 Yes it would be possible, I guess I was just thinking from the perspective of a user it would be nicer to have a much shorter code which could be used in the same manner! Thanks for your help.
tushar dhingra
@tushki
Apr 16 2015 18:27
@vkarpov15 could you help me out with the solution you had shared
Capture.PNG
this is the error i am getting could you help me with it ?
Valeri Karpov
@vkarpov15
Apr 16 2015 18:59
@feargal9 you can try appear.in's approach and pick 2-3 random dictionary words as your id, but really depends on your users and the general UX you want to have. Mongoose will let you do whatever you want :)
@tushki do you have another mongodb instance already running?
feargal9
@feargal9
Apr 16 2015 19:01
@vkarpov15 I didn't actually see that you mentioned the appear.in approach above. Thanks, I'll look into it now :)
is there anything in the docs about this approach?
Valeri Karpov
@vkarpov15
Apr 16 2015 19:18
nope - mongoose lets you define whatever schema you want, so your _id can be whatever you want and you can look up documents by whatever key you want. Nothing too crazy, just have a default that generates a readable string _id for you
feargal9
@feargal9
Apr 16 2015 19:20
Ah, I see. So I could have a schema 'TaskNumber' with one filed of type Number. Then have a field in my Task schema of type 'TaskNumber', and use this as the custom_id.
tushar dhingra
@tushki
Apr 16 2015 19:33
other instance running measn
?
Valeri Karpov
@vkarpov15
Apr 16 2015 23:37
@feargal9 yep
@tushki do you have a mongod process running?
err another mongod process running