PMXBOT Log file Viewer

Help | Karma | Search:

#mongodb logs for Wednesday the 23rd of January, 2019

(Back to #mongodb overview) (Back to channel listing) (Animate logs)
[04:27:36] <sash87> Hi Guys
[04:28:00] <sash87> I am new to mongoDb
[04:28:33] <sash87> Have one query ..on updating document in collections
[04:29:16] <sash87> My data is stored in the collection like below
[04:29:20] <sash87> https://www.irccloud.com/pastebin/SfZrzNkn/
[04:31:02] <sash87> I want to update the status to 3 where "_id" is that above id
[04:31:37] <sash87> below is the code I tried
[04:32:08] <sash87> https://www.irccloud.com/pastebin/hlqGJYQk/
[04:33:43] <sash87> I am getting the console output as "1 document got updated response is{"n":0,"nModified":0,"ok":1}"
[04:34:55] <sash87> but still my data is not updated
[04:34:57] <sash87> why ?
[04:47:42] <sash87> anyone ?
[05:16:04] <ronaldl93> Is this iso format correct for iso time?
[05:16:05] <ronaldl93> 2019-01-25T12:00:00
[05:16:13] <ronaldl93> I'm trying to query date range but no luck
[05:16:42] <ronaldl93> the db is saved in ISO time
[12:02:59] <maasha> How can I $addToSet roleId to roles in a document that looks like this: {_id, name, profile: { roles: [] }} ?
[12:03:08] <maasha> I fail getting the syntax right ...
[12:08:12] <maasha> This aint it: { profile: { $addToSet: { roles: roleId } } } and this aint it either: { $addToSet: { profile: { roles: roleId } } },
[12:25:56] <maasha> gotit
[12:26:00] <maasha> no thanks
[12:27:56] <sash87> how can I fetch the last record from mongo db in node.js
[12:28:00] <sash87> https://gist.github.com/sasmit/ae50347df8118c95bd6d9d14c6a3921f
[12:28:06] <sash87> my data is like this
[12:28:31] <sash87> I want to retrieve the last data
[12:29:56] <synthmeat> sash87: last inserted or newest by date?
[12:30:14] <sash87> last inserted ?
[12:30:19] <sash87> last inserted*
[12:30:40] <sash87> or newset by date both are same correct
[12:31:07] <sash87> because any time I push data to db it will store with new date
[12:31:27] <sash87> synthmeat: am I correct?
[12:32:12] <sash87> but basically I want the lastest record pushed to db
[12:32:28] <synthmeat> yes. it's not the best thing to rely on ObjectId, since it might change during the restore and such. so date it is...
[12:32:45] <synthmeat> if this is a frequent query, don't forget to add reverse date index like this...
[12:33:11] <sash87> lets say I have already A,B,C,D .. now I pushed E to db
[12:33:19] <sash87> so I should retrieve E
[12:33:22] <sash87> from DB
[12:33:25] <synthmeat> await db.createIndex('collectionName', { date: -1 }, { background: true });
[12:33:38] <synthmeat> then you can query (efficiently) with...
[12:34:07] <synthmeat> await db.collection('collectionName').find().sort({ date: -1 }).limit(1).toArray()
[12:34:38] <synthmeat> (ignore findOne and such methods, they're anti-pattern, imho)
[12:34:51] <sash87> ohh ok
[12:34:53] <synthmeat> so you'll get an empty on array with 1 element in after that
[12:35:20] <sash87> sorry I am complete new to mongo db
[12:35:28] <sash87> { date: -1 }, { background: true }
[12:35:33] <sash87> what are this
[12:36:04] <synthmeat> that's index creation. namely, mongodb will keep all the dates sorted in reverse in memory, so it can quickly find locations of the documents in question, if sorted by that index
[12:36:18] <sash87> ok
[12:36:41] <synthmeat> it just means "create index for colleciton 'collectionName' by inverse date sort, and do it in the background"
[12:37:07] <sash87> db.createIndex('collectionName', { date: -1 }, { background: true });
[12:37:07] <sash87> this command I should run from mongo shelll or from my node.js codebase
[12:37:09] <sash87> ?
[12:37:24] <synthmeat> this is straight up from node.js codebase, yeah
[12:37:34] <synthmeat> but it's the same in query lang too (i think)
[12:37:54] <synthmeat> query certainly is, not sure about index creation, i always manage indexes in code
[12:38:05] <synthmeat> (not sure is that the best idea, but whatever. it works)
[12:38:29] <synthmeat> anyways, if you're new to mongodb, install Compass and play with it, it's really fun and quick way to grok mongodb
[12:40:30] <sash87> db.createIndex('collectionName', { date: -1 }, { background: true });
[12:40:30] <sash87> this has to be executed everytime I push a new data to collection ?
[12:41:51] <synthmeat> no, you can manage it manually via direct queries on mongo shell or via Compass
[12:42:02] <sash87> ya thats correct
[12:42:10] <sash87> thats what I was thinking
[12:42:50] <synthmeat> it's super also useful to create aggregation pipelines, like this https://www.dropbox.com/s/ogjr6jrzftu30af/Screenshot%202019-01-23%2013.39.45.png?dl=0
[12:44:10] <sash87> ok ..little confsuing for me needs to spend time on this
[12:44:23] <sash87> for time being I will use index way
[12:44:47] <sash87> and let me check if I am getting the required result
[12:44:56] <sash87> thanks a lot synthmeat
[12:47:57] <sash87> synthmeat: I tried below command
[12:48:06] <sash87> https://www.irccloud.com/pastebin/texLY1FH/
[12:48:25] <sash87> I tried in mongo shell but its throwing error
[12:49:37] <Derick> sash87: is that the name of your database? Nagra...?
[12:49:53] <sash87> yes Derick
[12:50:18] <Derick> ah, then use: use NagraAutoBuildTool; and then: db.createIndex…
[12:50:22] <synthmeat> and what is the name of the colleciton?
[12:50:37] <sash87> I have this Telefonica_Build_Details collection
[12:50:38] <Derick> that's a good question too synthmeat
[12:50:45] <sash87> and I have already data in it
[12:50:57] <Derick> ok
[12:50:59] <Derick> so:
[12:51:02] <Derick> use NagraAutoBuildTool;
[12:51:11] <sash87> I am already using it
[12:51:14] <sash87> let me try again
[12:51:33] <Derick> db.Telefonica_Build_Details.createIndex({ date: -1 }, { background: true });
[12:51:37] <sash87> still same error
[12:51:55] <Derick> "the same error". I don't think so. Please show the output again.
[12:52:05] <sash87> i will try this
[12:52:15] <synthmeat> "stay awhile and listen"
[12:52:18] <sash87> I replied for the above comment
[12:52:33] <sash87> sorry
[12:53:10] <Bodenhaltung> synthmeat: Adrian the Witch?
[12:53:10] <sash87> now I tried the new command
[12:53:16] <sash87> https://www.irccloud.com/pastebin/3dfqKwf2/
[12:53:46] <synthmeat> Bodenhaltung: https://www.youtube.com/watch?v=VgULZIdQKhA
[12:53:57] <Bodenhaltung> I know it. :D
[12:54:09] <Derick> sash87: please read exactly what I wrote
[12:54:50] <sash87> https://www.irccloud.com/pastebin/TI0lOGfp/
[12:55:00] <sash87> thank you Derick
[12:55:01] <synthmeat> there you go
[12:55:25] <sash87> I thought db means I have to replace with db name
[12:55:48] <synthmeat> then "use" would make no sense
[12:56:38] <sash87> yes exactly
[12:57:26] <sash87> then synthmeat , what comamnd you gave would have worked for me as well
[12:57:46] <sash87> I was always using the db name instead of only db keyword
[12:59:34] <sash87> synthmeat: so the other code you shared
[12:59:37] <sash87> db.collection('collectionName').find().sort({ date: -1 }).limit(1).toArray()
[12:59:50] <sash87> I will try now
[13:00:30] <synthmeat> db.collection('Telefonica_Build_Details').find().sort({ date: -1 }).limit(1).toArray()
[13:00:59] <sash87> thanks guys so much of help
[13:08:02] <sash87> synthmeat:
[13:08:18] <sash87> do I need to pass {} inside find
[13:08:32] <synthmeat> i'm not sure
[13:08:45] <sash87> https://www.irccloud.com/pastebin/j0VNsdhA/
[13:08:56] <sash87> I saw here
[13:08:57] <sash87> https://www.w3schools.com/nodejs/nodejs_mongodb_find.asp
[13:09:06] <sash87> they are using {}
[13:09:21] <synthmeat> sure, whatever works
[13:09:45] <sash87> ok
[13:52:28] <sash87> Hi synthmeat
[13:52:49] <sash87> synthmeat: one more error I am getting now
[13:52:56] <sash87> collection.find({}).sort({ date: -1 }).limit(1).toArray( function(err, response) {
[13:53:00] <sash87> this worked for me
[13:53:36] <sash87> so what I am doing now once I get the response , i am using reponse id to fetch some data from one more collection
[13:53:56] <sash87> but that is throwing me error "MongoError: Topology was destroyed"
[13:54:08] <sash87> below is my code
[13:54:12] <sash87> https://www.irccloud.com/pastebin/ICuDwv79/
[15:17:14] <synthmeat> sash87: you're driver is disconnecting. you aren't handling disconnections or are handling them wrong. can be multitiude of things.
[15:17:21] <synthmeat> *your
[15:18:33] <synthmeat> hook up to all the possible client's events and log them out to debug