last day (15 days later) » 

4:56 PM
1
A: Finding/Counting Duplicate Values in Array in MongoDB

whoamiTry below query : Query : db.collection.aggregate([ /** project only needed fields & transform fields as you like */ { $project: { customer_id: "$viewer_id", enterprise_id: "$subscribed_channels.channel_id", count: { /** Subtract size of original array & newly ...

 
hi @whoami . executing the above query gives error as Error: command failed: { "ok" : 0, "errmsg" : "The argument to $size must be an array, but was of type: missing", "code" : 17124, "codeName" : "Location17124" } : aggregate failed . are my documents corrupt ?
 
@AjinkyaKarode : Yes, I think some of your docs doesn't have subscribed_channels as array. Can you see if that's correct ? What do you wanted to do on those ?
 
hi @whoami Sorry, i just verified all documents and yes in some docs there is no subscribed_channel field since that viewer has no subscription. How can i handle such docs ?
 
@AjinkyaKarode : what do you've to do to those docs ? Do you want to remove those from result ?
 
Yes @whoami :- not take those docs into consideration
 
4:56 PM
@AjinkyaKarode : Ok all you need to do is to filter those out using $match, try this { $match: { subscribed_channels: { $type: "array" } } } or test it here :: mongoplayground.net/p/Ds57XGvVp6p
 
Thanks a lot brother @whoami , It worked. Just one last help I need only those results which has count more than 1 , because i am getting results with count as -1,-2. Also i ll try to search this on internet . Your command over mongodb is excellent , Do you have any tutorials for such complex mongodb queries? I will pay for them if needed
 
@AjinkyaKarode : :-) Ok going back to my initial question Do you need docs that has duplicates ? Or Do you need docs that has duplicates along with count ? Redg. tutorials you can find those here :: university.mongodb.com (Free & directly from Mongo)
 
Just verfied the output, i have not got any docs with count greater than 0.
 
@AjinkyaKarode :What is the issue ? What has differed from test playground ?
 
Hi @whoami .Thanks for all your efforts,Just verfied the output. mongoplayground.net/p/GxtKlE0fkst and i got count more than 0. I ll check out the website you recommended, but also if i want to learn from you is it possible?any email id or linkedin?
 
4:56 PM
@AjinkyaKarode : Yes if you want docs with dups, you can use another $match at end & filter docs with count >0
@AjinkyaKarode : Nope even I'm kind of new, started MongoDB over an year, So you should probably in same stage within no time, unfortunately I'm not in a level to teach, but certainly can help if I can, you can ping me in SOF :-) al d bst..
 
Ok @whoami , so i can ping you by using @whoami in comments ??
Also university of mongodb has Introductory course on aggregation. Should i take udemy courses on MongoDB for advanced concepts
 
5:15 PM
yes
you can directly take on unit courses
later on the go u can go on other courses
 

last day (15 days later) »