MongoDB $graphlookup to link Twitter user account names with user mentions - mongodb

A sample document with user mentions in the tweets dataset I'm working with looks like this. I'm trying to get mongoDB to link the user.screen_name with their mentions in other tweets, if there are any, and go further up the hierarchy if that tweet also mentions someone else.
{
_id: ObjectId("5c8eccb2caa187d17ca6b214"),
text: 'PROMO - Concorra a um Ipad por semana #sorteio Faça seu cadastro e envie seu login para #lepetitpromo RT pls..',
in_reply_to_status_id: null,
retweet_count: null,
contributors: null,
created_at: 'Thu Sep 02 19:30:31 +0000 2010',
geo: null,
source: 'web',
coordinates: null,
in_reply_to_screen_name: null,
truncated: false,
entities: {
user_mentions: [
{
indices: [ 88, 101 ],
screen_name: 'lepetitpromo',
name: 'Le Petit Promo',
id: 116353452
}
],
urls: [],
hashtags: [ { text: 'sorteio', indices: [ 38, 46 ] } ]
},
retweeted: false,
place: null,
user: {
friends_count: 39464,
profile_sidebar_fill_color: 'A32247',
location: 'SP, Brasil',
verified: false,
follow_request_sent: null,
favourites_count: 546,
profile_sidebar_border_color: 'e69cd7',
profile_image_url: 'http://a1.twimg.com/profile_images/1112836301/30480_107192455999355_100001258033161_53885_3288503_n_normal.jpg',
geo_enabled: true,
created_at: 'Mon Feb 22 04:34:35 +0000 2010',
description: 'Promonauta ligada 24hs nas melhores ofertas, sorteios e novidades no TWITTER! - Site http://www.lepetitpromo.com.br - contato: lepetitpromo#gmail.com',
time_zone: 'Brasilia',
url: 'http://lepetitpromo.com.br',
screen_name: 'lepetitpromo',
notifications: null,
profile_background_color: '000000',
listed_count: 683,
lang: 'en',
profile_background_image_url: 'http://a3.twimg.com/profile_background_images/141124445/x5293307ec686ccde4688a7b20c51d5a.jpg',
statuses_count: 49358,
following: null,
profile_text_color: 'b393b3',
protected: false,
show_all_inline_media: false,
profile_background_tile: true,
name: 'Le Petit Promo',
contributors_enabled: false,
profile_link_color: '60a9d1',
followers_count: 57774,
id: 116353452,
profile_use_background_image: true,
utc_offset: -10800
},
favorited: false,
in_reply_to_user_id: null,
id: Long("22824145700")
}
Here's the query I have so far:
db.tweets.aggregate(
[
{
$graphLookup: {
from: 'tweets',
startWith: "$user.screen_name",
connectFromField: 'user.screen_name',
connectToField: 'entities.user_mentions.screen_name',
as: 'User Mentions'
}
}
]
)
But the code above still doesn't return anything, I'm not really sure what I'm getting wrong but any help would be much appreciated.

Related

what simple mapReduce function can I get out a of a tweets dataset like this?

I'm learning mongoDB and there's an exercise where we have to come up with our own scenario and make a query based on that description. Problem is I'm not really clear on what or how to use mapReduce effectively, so I created this aggregation query and hopefully someone can point me to a similar way I can do it but with mapReduce?
The dataset of tweets I'm working with looks generally looks like this:
{
_id: ObjectId("5c8eccb0caa187d17ca623fe"),
text: "#AdmireBiebs what ya think about to change my name to #NickJMunroC? I want one with them both. they're my imaginary husbands :P",
in_reply_to_status_id: null,
retweet_count: null,
contributors: null,
created_at: 'Thu Sep 02 18:11:24 +0000 2010',
geo: null,
source: 'Power Twitter',
coordinates: null,
in_reply_to_screen_name: 'AdmireBiebs',
truncated: false,
entities: {
user_mentions: [
{
indices: [ 0, 12 ],
screen_name: 'AdmireBiebs',
name: 'with ari ♥',
id: 64392486
}
],
urls: [],
hashtags: []
},
retweeted: false,
place: null,
user: {
friends_count: 82,
profile_sidebar_fill_color: 'FFFFFF',
location: 'Barcelona, S p a i n ',
verified: false,
follow_request_sent: null,
favourites_count: 3,
profile_sidebar_border_color: 'E5C4AC',
profile_image_url: 'http://a1.twimg.com/profile_images/1080450837/MileyIcon_normal.jpg',
geo_enabled: false,
created_at: 'Sun Aug 09 21:21:31 +0000 2009',
description: "I Support #nickjonas & #jonasbrothers 'til the end. NILEY FOR THE WIN. ",
time_zone: 'Greenland',
url: null,
screen_name: 'TheNickJCrew',
notifications: null,
profile_background_color: 'DAE8EB',
listed_count: 51,
lang: 'en',
profile_background_image_url: 'http://a3.twimg.com/profile_background_images/124098229/xa2b1612896a14878a1008db61390773.png',
statuses_count: 808,
following: null,
profile_text_color: 'ababab',
protected: false,
show_all_inline_media: false,
profile_background_tile: true,
name: 'Ariadna',
contributors_enabled: false,
profile_link_color: 'c4ad8f',
followers_count: 219,
id: 64257227,
profile_use_background_image: true,
utc_offset: -10800
},
favorited: false,
in_reply_to_user_id: 64392486,
id: Long("22819397900")
},
{
_id: ObjectId("5c8eccb0caa187d17ca62408"),
text: 'RT #Philanthropy: How should nonprofit groups measure their social-media efforts? A new podcast from #afine http://ht.ly/2yFlS',
in_reply_to_status_id: null,
retweet_count: null,
contributors: null,
created_at: 'Thu Sep 02 18:11:24 +0000 2010',
geo: null,
source: 'web',
retweeted_status: {
text: 'How should nonprofit groups measure their social-media efforts? A new podcast from #afine http://ht.ly/2yFlS',
in_reply_to_status_id: null,
retweet_count: null,
contributors: null,
created_at: 'Thu Sep 02 18:09:14 +0000 2010',
geo: null,
source: 'HootSuite',
coordinates: null,
in_reply_to_screen_name: null,
truncated: false,
entities: {
user_mentions: [
{
indices: [ 83, 89 ],
screen_name: 'Afine',
name: 'Allison Fine',
id: 15016599
}
],
urls: [
{
indices: [ 90, 108 ],
url: 'http://ht.ly/2yFlS',
expanded_url: null
}
],
hashtags: []
},
retweeted: false,
place: null,
user: {
friends_count: 2800,
profile_sidebar_fill_color: 'faf5eb',
location: 'Washington, D.C.',
verified: false,
follow_request_sent: null,
favourites_count: 2,
profile_sidebar_border_color: 'faf5eb',
profile_image_url: 'http://a2.twimg.com/profile_images/669725690/twitterprofilepic_normal.jpg',
geo_enabled: false,
created_at: 'Wed Dec 05 19:13:58 +0000 2007',
description: "The Chronicle of Philanthropy's official Twitter page, updated by Peter Panepento",
time_zone: 'Central Time (US & Canada)',
url: 'http://philanthropy.com',
screen_name: 'Philanthropy',
notifications: null,
profile_background_color: 'e7dfd3',
listed_count: 1679,
lang: 'en',
profile_background_image_url: 'http://a3.twimg.com/profile_background_images/71652755/bgv3.jpg',
statuses_count: 6492,
following: null,
profile_text_color: '572b0e',
protected: false,
show_all_inline_media: false,
profile_background_tile: false,
name: 'Philanthropy',
contributors_enabled: false,
profile_link_color: '246a88',
followers_count: 19406,
id: 10880202,
profile_use_background_image: true,
utc_offset: -21600
},
favorited: false,
in_reply_to_user_id: null,
id: Long("22819258393")
},
coordinates: null,
in_reply_to_screen_name: null,
truncated: false,
entities: {
user_mentions: [
{
indices: [ 3, 16 ],
screen_name: 'Philanthropy',
name: 'Philanthropy',
id: 10880202
},
{
indices: [ 101, 107 ],
screen_name: 'Afine',
name: 'Allison Fine',
id: 15016599
}
],
urls: [
{
indices: [ 108, 126 ],
url: 'http://ht.ly/2yFlS',
expanded_url: null
}
],
hashtags: []
},
retweeted: false,
place: null,
user: {
friends_count: 68,
profile_sidebar_fill_color: 'e0ff92',
location: '',
verified: false,
follow_request_sent: null,
favourites_count: 0,
profile_sidebar_border_color: '87bc44',
profile_image_url: 'http://a1.twimg.com/profile_images/1101368689/3f84a22_normal.jpg',
geo_enabled: false,
created_at: 'Thu Jul 15 03:54:39 +0000 2010',
description: '',
time_zone: null,
url: null,
screen_name: 'DerekBechthold',
notifications: null,
profile_background_color: '9ae4e8',
listed_count: 1,
lang: 'en',
profile_background_image_url: 'http://s.twimg.com/a/1282328015/images/themes/theme1/bg.png',
statuses_count: 54,
following: null,
profile_text_color: '000000',
protected: false,
show_all_inline_media: false,
profile_background_tile: false,
name: 'Derek Bechthold',
contributors_enabled: false,
profile_link_color: '0000ff',
followers_count: 24,
id: 166846185,
profile_use_background_image: true,
utc_offset: null
},
favorited: false,
in_reply_to_user_id: null,
id: Long("22819398100")
},
{
_id: ObjectId("5c8eccb0caa187d17ca62415"),
text: 'Photo: porque nada se compara com o simples fato de ser feliz . http://tumblr.com/xhxhfo113',
in_reply_to_status_id: null,
retweet_count: null,
contributors: null,
created_at: 'Thu Sep 02 18:11:27 +0000 2010',
geo: null,
source: 'Tumblr',
coordinates: null,
in_reply_to_screen_name: null,
truncated: false,
entities: {
user_mentions: [],
urls: [
{
indices: [ 64, 91 ],
url: 'http://tumblr.com/xhxhfo113',
expanded_url: null
}
],
hashtags: []
},
retweeted: false,
place: null,
user: {
friends_count: 379,
profile_sidebar_fill_color: 'f7e8f3',
location: 'rio de janeiro',
verified: false,
follow_request_sent: null,
favourites_count: 6,
profile_sidebar_border_color: 'd69fc8',
profile_image_url: 'http://a1.twimg.com/profile_images/1039533261/a_normal.JPG',
geo_enabled: false,
created_at: 'Wed Jun 30 17:03:39 +0000 2010',
description: '',
time_zone: 'Brasilia',
url: 'http://twitpic.com/photos/amanda_raibolt',
screen_name: 'amanda_raibolt',
notifications: null,
profile_background_color: 'fcfcfc',
listed_count: 2,
lang: 'en',
profile_background_image_url: 'http://a3.twimg.com/profile_background_images/118456393/2.jpg',
statuses_count: 562,
following: null,
profile_text_color: 'f771b4',
protected: false,
show_all_inline_media: false,
profile_background_tile: false,
name: 'Raibolt Amanda ! ',
contributors_enabled: false,
profile_link_color: 'c95799',
followers_count: 86,
id: 161359013,
profile_use_background_image: true,
utc_offset: -10800
},
favorited: false,
in_reply_to_user_id: null,
id: Long("22819400700")
},
{
_id: ObjectId("5c8eccb0caa187d17ca62419"),
text: 'えっ?',
in_reply_to_status_id: null,
retweet_count: null,
contributors: null,
created_at: 'Thu Sep 02 18:11:27 +0000 2010',
geo: null,
source: 'twittbot.net',
coordinates: null,
in_reply_to_screen_name: null,
truncated: false,
entities: { user_mentions: [], urls: [], hashtags: [] },
retweeted: false,
place: null,
user: {
friends_count: 27,
profile_sidebar_fill_color: 'e0ff92',
location: '東京湾',
verified: false,
follow_request_sent: null,
favourites_count: 0,
profile_sidebar_border_color: '87bc44',
profile_image_url: 'http://a2.twimg.com/profile_images/1013384238/___________normal.jpg',
geo_enabled: false,
created_at: 'Tue Jun 22 09:10:16 +0000 2010',
description: '海老天こと東風(ID:lobster090)のBOTです。\r\nたまに海老天/A.B.十/東風でつぶやいてきやがります。',
time_zone: 'Hawaii',
url: null,
screen_name: 'lobster090_bot',
notifications: null,
profile_background_color: '9ae4e8',
listed_count: 1,
lang: 'ja',
profile_background_image_url: 'http://s.twimg.com/a/1281738360/images/themes/theme1/bg.png',
statuses_count: 1569,
following: null,
profile_text_color: '000000',
protected: false,
show_all_inline_media: false,
profile_background_tile: false,
name: '海老bot',
contributors_enabled: false,
profile_link_color: '0000ff',
followers_count: 19,
id: 158301291,
profile_use_background_image: true,
utc_offset: -36000
},
favorited: false,
in_reply_to_user_id: null,
id: Long("22819401300")
And this is the aggregation query I have that basically looks at the user.screen_name of each tweet and count how many times each user tweets in the whole dataset, and sort that based on amount of tweets.
db.tweets.aggregate([{$group: {_id: "$user.screen_name", totalTweets: {$count: {}}}}, {$sort: {totalTweets: -1}}])
And the output looks like this
{ _id: 'JBTeenageDream', totalTweets: 6 },
{ _id: 'Jobs_SanAntonio', totalTweets: 6 },
{ _id: 'vslxo', totalTweets: 5 },
{ _id: 'mysterytrick', totalTweets: 4 },
{ _id: 'djmangueira', totalTweets: 4 },
{ _id: 'JPguedas', totalTweets: 4 }
So what's a way to do that but with mapReduce?

MongoDB $graphLookup to find nested tweets hierarchy?

I'm practicing mongoDB with a small tweets dataset right now and don't have much experience with $graphLookup. For each document that matches, I'm trying to find the screen_name of the user the tweet is replying to, and if that tweet is also a retweet of another tweet, then display the user screen_name of that tweet too, so on and so forth up the hierarchy of retweets until the original tweet.
A standard document of retweets in the dataset looks like this
`
{
_id: ObjectId("5c8eccb0caa187d17ca62408"),
text: 'RT #Philanthropy: How should nonprofit groups measure their social-media efforts? A new podcast from #afine http://ht.ly/2yFlS',
in_reply_to_status_id: null,
retweet_count: null,
contributors: null,
created_at: 'Thu Sep 02 18:11:24 +0000 2010',
geo: null,
source: 'web',
retweeted_status: {
text: 'How should nonprofit groups measure their social-media efforts? A new podcast from #afine http://ht.ly/2yFlS',
in_reply_to_status_id: null,
retweet_count: null,
contributors: null,
created_at: 'Thu Sep 02 18:09:14 +0000 2010',
geo: null,
source: 'HootSuite',
coordinates: null,
in_reply_to_screen_name: null,
truncated: false,
entities: {
user_mentions: [
{
indices: [ 83, 89 ],
screen_name: 'Afine',
name: 'Allison Fine',
id: 15016599
}
],
urls: [
{
indices: [ 90, 108 ],
url: 'http://ht.ly/2yFlS',
expanded_url: null
}
],
hashtags: []
},
retweeted: false,
place: null,
user: {
friends_count: 2800,
profile_sidebar_fill_color: 'faf5eb',
location: 'Washington, D.C.',
verified: false,
follow_request_sent: null,
favourites_count: 2,
profile_sidebar_border_color: 'faf5eb',
profile_image_url: 'http://a2.twimg.com/profile_images/669725690/twitterprofilepic_normal.jpg',
geo_enabled: false,
created_at: 'Wed Dec 05 19:13:58 +0000 2007',
description: "The Chronicle of Philanthropy's official Twitter page, updated by Peter Panepento",
time_zone: 'Central Time (US & Canada)',
url: 'http://philanthropy.com',
screen_name: 'Philanthropy',
notifications: null,
profile_background_color: 'e7dfd3',
listed_count: 1679,
lang: 'en',
profile_background_image_url: 'http://a3.twimg.com/profile_background_images/71652755/bgv3.jpg',
statuses_count: 6492,
following: null,
profile_text_color: '572b0e',
protected: false,
show_all_inline_media: false,
profile_background_tile: false,
name: 'Philanthropy',
contributors_enabled: false,
profile_link_color: '246a88',
followers_count: 19406,
id: 10880202,
profile_use_background_image: true,
utc_offset: -21600
},
favorited: false,
in_reply_to_user_id: null,
id: Long("22819258393")
},
coordinates: null,
in_reply_to_screen_name: null,
truncated: false,
entities: {
user_mentions: [
{
indices: [ 3, 16 ],
screen_name: 'Philanthropy',
name: 'Philanthropy',
id: 10880202
},
{
indices: [ 101, 107 ],
screen_name: 'Afine',
name: 'Allison Fine',
id: 15016599
}
],
urls: [
{
indices: [ 108, 126 ],
url: 'http://ht.ly/2yFlS',
expanded_url: null
}
],
hashtags: []
},
retweeted: false,
place: null,
user: {
friends_count: 68,
profile_sidebar_fill_color: 'e0ff92',
location: '',
verified: false,
follow_request_sent: null,
favourites_count: 0,
profile_sidebar_border_color: '87bc44',
profile_image_url: 'http://a1.twimg.com/profile_images/1101368689/3f84a22_normal.jpg',
geo_enabled: false,
created_at: 'Thu Jul 15 03:54:39 +0000 2010',
description: '',
time_zone: null,
url: null,
screen_name: 'DerekBechthold',
notifications: null,
profile_background_color: '9ae4e8',
listed_count: 1,
lang: 'en',
profile_background_image_url: 'http://s.twimg.com/a/1282328015/images/themes/theme1/bg.png',
statuses_count: 54,
following: null,
profile_text_color: '000000',
protected: false,
show_all_inline_media: false,
profile_background_tile: false,
name: 'Derek Bechthold',
contributors_enabled: false,
profile_link_color: '0000ff',
followers_count: 24,
id: 166846185,
profile_use_background_image: true,
utc_offset: null
},
favorited: false,
in_reply_to_user_id: null,
id: Long("22819398100")
},
{
_id: ObjectId("5c8eccb0caa187d17ca624ab"),
text: 'RT #ESPNsoccernet: Van der Vaart denies being a failure at Real Madrid: Rafael van der Vaart maintains he was no "failure" at Real Ma... ...',
in_reply_to_status_id: null,
retweet_count: null,
contributors: null,
created_at: 'Thu Sep 02 18:11:44 +0000 2010',
geo: null,
source: 'Twitter for iPhone',
retweeted_status: {
text: 'Van der Vaart denies being a failure at Real Madrid: Rafael van der Vaart maintains he was no "failure" at Real Ma... http://bit.com/9d3Gah',
in_reply_to_status_id: null,
retweet_count: null,
contributors: null,
created_at: 'Thu Sep 02 16:53:41 +0000 2010',
geo: null,
source: 'twitterfeed',
coordinates: null,
in_reply_to_screen_name: null,
truncated: false,
entities: {
user_mentions: [],
urls: [
{
indices: [ 118, 138 ],
url: 'http://bit.com/9d3Gah',
expanded_url: null
}
],
hashtags: []
},
retweeted: false,
place: null,
user: {
friends_count: 0,
profile_sidebar_fill_color: 'e0ff92',
location: null,
verified: false,
follow_request_sent: null,
favourites_count: 0,
profile_sidebar_border_color: '87bc44',
profile_image_url: 'http://a2.twimg.com/profile_images/70131430/espn_normal.jpg',
geo_enabled: false,
created_at: 'Wed Jan 07 15:27:26 +0000 2009',
description: null,
time_zone: null,
url: null,
screen_name: 'ESPNsoccernet',
notifications: null,
profile_background_color: '9ae4e8',
listed_count: 368,
lang: 'en',
profile_background_image_url: 'http://s.twimg.com/a/1282351897/images/themes/theme1/bg.png',
statuses_count: 13447,
following: null,
profile_text_color: '000000',
protected: false,
show_all_inline_media: false,
profile_background_tile: false,
name: 'ESPNsoccernet',
contributors_enabled: false,
profile_link_color: '0000ff',
followers_count: 4669,
id: 18724620,
profile_use_background_image: true,
utc_offset: null
},
favorited: false,
in_reply_to_user_id: null,
id: Long("22814093566")
},
coordinates: null,
in_reply_to_screen_name: null,
truncated: true,
entities: {
user_mentions: [
{
indices: [ 3, 17 ],
screen_name: 'ESPNsoccernet',
name: 'ESPNsoccernet',
id: 18724620
}
],
urls: [],
hashtags: []
},
retweeted: false,
place: null,
user: {
friends_count: 54,
profile_sidebar_fill_color: 'e0ff92',
location: 'Park Triangle Apartments (US)',
verified: false,
follow_request_sent: null,
favourites_count: 2,
profile_sidebar_border_color: '87bc44',
profile_image_url: 'http://a0.twimg.com/profile_images/1066211448/27399ece-83d9-4187-bf0d-04f538d7725c_normal.png',
geo_enabled: false,
created_at: 'Mon Apr 28 23:28:31 +0000 2008',
description: '',
time_zone: 'Quito',
url: null,
screen_name: 'desjef75',
notifications: null,
profile_background_color: '9ae4e8',
listed_count: 0,
lang: 'en',
profile_background_image_url: 'http://s.twimg.com/a/1283397887/images/themes/theme1/bg.png',
statuses_count: 1031,
following: null,
profile_text_color: '000000',
protected: false,
show_all_inline_media: false,
profile_background_tile: false,
name: 'desjef75',
contributors_enabled: false,
profile_link_color: '0000ff',
followers_count: 15,
id: 14577356,
profile_use_background_image: true,
utc_offset: -18000
},
favorited: false,
in_reply_to_user_id: null,
id: Long("22819419400")
}
`
This is the query I have so far
db.tweets.aggregate([{$graphLookup:{from: "tweets", startWith: "$text", connectFromField:"text", connectToField:"retweeted_status.text", as:"Retweets Hierarchy"}}])
But this isn't really showing the screen_name, I'm not really even sure how to that while still matching the text to the fields and I'm so lost any help would be much appreciated.
My ideal result would be something like the image below, where it would show a clear structure from the bottom up.

How to get a field of an object in document?

I am not sure how to phrase this question properly but basically I have an "Order" Schema and each order model contains an array of product objects created using "Product" Schema
When I create an order, my req.body is this
body: {
orderItems: [ [Object] ],
shippingAddress: {
fullName: '123',
address: '1231',
city: '123',
postalCode: '123',
country: '123'
},
shippingPrice: 0,
paymentMethod: 'Stripe',
itemsPrice: 338,
totalPrice: 338
},
If I log req.body.orderItems, I can see each product object printed
{
_id: new ObjectId("62d51a3895cad3ca283302f3"),
name: 'Christmas Cake',
slug: 'christmas-cake',
image: '/images/cake.jpg',
shop: 'Custom Cakes',
category: 'food',
description: 'Custom baked christmas cake, pre-order needed.',
price: 70,
stock: 1,
rating: 3,
numReviews: 2,
__v: 2,
createdAt: 2022-07-18T08:30:48.931Z,
updatedAt: 2022-07-20T03:03:00.592Z,
}
{
_id: new ObjectId("62d7a8c126dcacfc13055e3d"),
name: 'cake',
slug: 'cake',
image: '/images/cake.jpg',
shop: 'Custom Cakes',
category: 'food',
description: 'cake',
price: 15,
stock: 2,
rating: 0,
numReviews: 0,
user: { _id: new ObjectId("62d51d57c08cd7e6675e8d45")},
reviews: [],
createdAt: 2022-07-20T07:03:29.372Z,
updatedAt: 2022-07-20T07:03:59.315Z,
__v: 0
}
But I am unable to obtain the 'shop' field. req.body.orderItems.shop returns undefined

$group and $lookup is not giving proper data

I am having code like this
const aggregate = [];
const lookup = {
$lookup: {
from: "balances",
localField: "accountNumber",
foreignField: "account_id",
as: "bankbalance",
},
};
const unwind = {
$unwind: "$bankbalance",
};
const match = {
$match: {
"bankbalance.date": {
$gte: startDate,
$lte: endDate,
},
userId: mongoose.Types.ObjectId(userId),
isActive: 1,
type: "accountType"
},
};
const group = {
$group: {
_id: '$_id',
accountNumber:{"$first":"$accountNumber"},
balances: {
$addToSet: { date: "$$ROOT.date", amount: "$$ROOT.amount" },
},
},
};
aggregate.push(lookup);
aggregate.push(unwind);
aggregate.push(group);
aggregate.push(match);
let data = await BankAccounts.aggregate(aggregate);
this code is giving me empty [] data, if I do not use $group then its giving data like this
{
_id: 611bc2c4f9649b2c6fe4007f,
bankId: 'string',
mask: '0000',
name: 'Plaid Checking',
userId: 611bc2794d9f391bf2cd7877,
accountNumber: 'xxxxxxxxxxxxxxxxxxx',
bankTokenId: xxxxxxxxxxxxxxxxxxxxxxxxxxx,
createdAt: 2021-08-17T14:08:04.058Z,
isConnected: true,
subtype: 'checking',
type: 'depository',
updatedAt: 2021-08-25T11:36:37.824Z,
isActive: 1,
bankbalance: {
_id: 61262b9bf9649b2c6fe7d67c,
ISODate: '2021-05-19T00:00:00.000Z',
account_id: 'xxxxxxxxxxxxxxxxxxxxxxxx',
userId: 611bc2794d9f391bf2cd7877,
ISOCountryCode: null,
accountId: xxxxxxxxxxxxxxxxxxxx,
amount: -1047.62,
bankTokenId: xxxxxxxxxxxxxxxxxxxx,
createdAt: 2021-08-25T11:37:26.960Z,
current: null,
date: '2021-05-19',
institutionId: xxxxxxxxxxxxxxxxxxxxxx,
isActive: 1,
unofficialCountryCode: null,
updatedAt: 2021-08-25T11:37:26.960Z
}
},
{
_id: 611bc2c4f9649b2c6fe4007f,
bankId: 'string',
mask: '0000',
name: 'Plaid Checking',
userId: 611bc2794d9f391bf2cd7877,
accountNumber: 'xxxxxxxxxxxxxxxxxxx',
bankTokenId: xxxxxxxxxxxxxxxxxxxxxxxxxxx,
createdAt: 2021-08-17T14:08:04.058Z,
isConnected: true,
subtype: 'checking',
type: 'depository',
updatedAt: 2021-08-25T11:36:37.824Z,
isActive: 1,
bankbalance: {
_id: 61262b99f9649b2c6fe7d671,
ISODate: '2021-05-24T00:00:00.000Z',
account_id: 'xxxxxxxxxxxxxxxxxxxxxxxx',
userId: 611bc2794d9f391bf2cd7877,
ISOCountryCode: null,
accountId: xxxxxxxxxxxxxxxxxxxx,
amount: -1137.02,
bankTokenId: xxxxxxxxxxxxxxxxxxxx,
createdAt: 2021-08-25T11:37:26.960Z,
current: null,
date: '2021-05-24',
institutionId: xxxxxxxxxxxxxxxxxxxxxx,
isActive: 1,
unofficialCountryCode: null,
updatedAt: 2021-08-25T11:37:26.960Z
}
}
this is giving all data record again and again, here reference between 2 collection is accountNumber and account_id. I want to group all records in 1 like
{
_id: 611bc2c4f9649b2c6fe4007f,
bankId: 'string',
mask: '0000',
name: 'Plaid Checking',
userId: 611bc2794d9f391bf2cd7877,
accountNumber: 'xxxxxxxxxxxxxxxxxxx',
bankTokenId: xxxxxxxxxxxxxxxxxxxxxxxxxxx,
createdAt: 2021-08-17T14:08:04.058Z,
isConnected: true,
subtype: 'checking',
type: 'depository',
updatedAt: 2021-08-25T11:36:37.824Z,
isActive: 1,
bankbalance: [{
_id: 61262b99f9649b2c6fe7d671,
ISODate: '2021-05-24T00:00:00.000Z',
account_id: 'xxxxxxxxxxxxxxxxxxxxxxxx',
userId: 611bc2794d9f391bf2cd7877,
ISOCountryCode: null,
accountId: xxxxxxxxxxxxxxxxxxxx,
amount: -1137.02,
bankTokenId: xxxxxxxxxxxxxxxxxxxx,
createdAt: 2021-08-25T11:37:26.960Z,
current: null,
date: '2021-05-24',
institutionId: xxxxxxxxxxxxxxxxxxxxxx,
isActive: 1,
unofficialCountryCode: null,
updatedAt: 2021-08-25T11:37:26.960Z
},
{
_id: 61262b9bf9649b2c6fe7d67c,
ISODate: '2021-05-19T00:00:00.000Z',
account_id: 'xxxxxxxxxxxxxxxxxxxxxxxx',
userId: 611bc2794d9f391bf2cd7877,
ISOCountryCode: null,
accountId: xxxxxxxxxxxxxxxxxxxx,
amount: -1047.62,
bankTokenId: xxxxxxxxxxxxxxxxxxxx,
createdAt: 2021-08-25T11:37:26.960Z,
current: null,
date: '2021-05-19',
institutionId: xxxxxxxxxxxxxxxxxxxxxx,
isActive: 1,
unofficialCountryCode: null,
updatedAt: 2021-08-25T11:37:26.960Z
}
]
}
also sort bankbalance by date. Any help! Thanks in advance!!
The stages are in the order
aggregate.push(lookup);
aggregate.push(unwind);
aggregate.push(group);
aggregate.push(match);
When the group stage executes, each document will have only the fields noted in that stage. i.e.
{
_id: 611bc2c4f9649b2c6fe4007f,
accountNumber: 'xxxxxxxxxxxxxxxxxxx',
balances: [ {date: ......, amount: .....} ]
}
When the match stage executes, the fields "bankbalance.date", userId, isActive, and type don't exist, so no documents satisfy the match.

Mongoose findbyid / find({id: "..."}) returning Null

I have seen this asked several times, but I haven't found a solution that has worked. I am trying to query a MongoDB using Mongoose .findById and am not getting the expected results. find({id: "..."}) is also not returning anything.
Using find without any parameters displays all of the expected results including id key-value pairs, but I cannot use the id value in the query.
Using mongoose: 5.4.9
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mongo-exercises', { useNewUrlParser: true });
const courseSchema = new mongoose.Schema({
name: String,
author: String,
tags: [String],
date: { type: Date, default: Date.now },
isPublished: Boolean,
price: Number
});
const Course = mongoose.model('Course', courseSchema);
async function getCourses() {
return await Course
.find()
}
async function run() {
const result = await getCourses();
console.log(result);
}
run();
//Return
[ { tags: [ 'react', 'frontend' ],
_id: 5a68fdf95db93f6477053ddd,
date: 2018-01-24T21:43:21.589Z,
name: 'React Course',
author: 'Mosh',
isPublished: false,
__v: 0 },
{ tags: [ 'aspnet', 'backend' ],
_id: 5a68fde3f09ad7646ddec17e,
date: 2018-01-24T21:42:59.605Z,
name: 'ASP.NET MVC Course',
author: 'Mosh',
isPublished: true,
price: 15,
__v: 0 },
{ tags: [ 'node', 'backend' ],
_id: 5a68fe2142ae6a6482c4c9cb,
date: 2018-01-24T21:44:01.075Z,
name: 'Node.js Course by Jack',
author: 'Jack',
isPublished: true,
price: 12,
__v: 0 },
{ tags: [ 'node', 'backend' ],
_id: 5a68fdd7bee8ea64649c2777,
date: 2018-01-24T21:42:47.912Z,
name: 'Node.js Course',
author: 'Mosh',
isPublished: true,
price: 20,
__v: 0 },
{ tags: [ 'node', 'backend' ],
_id: 5a68ff090c553064a218a547,
date: 2018-01-24T21:47:53.128Z,
name: 'Node.js Course by Mary',
author: 'Mary',
isPublished: false,
price: 12,
__v: 0 },
{ tags: [ 'angular', 'frontend' ],
_id: 5a6900fff467be65019a9001,
date: 2018-01-24T21:56:15.353Z,
name: 'Angular Course',
author: 'Mosh',
isPublished: true,
price: 15,
__v: 0 },
{ tags: [ 'express', 'backend' ],
_id: 5a68fdc3615eda645bc6bdec,
date: 2018-01-24T21:42:27.388Z,
name: 'Express.js Course',
author: 'Mosh',
isPublished: true,
price: 10,
__v: 0 } ]
That code verifies I'm connected to the correct database and retrieving real ids. When I modify the getCourses function as shown below, I get null or an empty array depending on whether I use findById or find({id: "..."}).
async function getCourses() {
return await Course
.findById('5a68fdf95db93f6477053ddd')
}
//null
async function getCourses() {
return await Course
.find({ id: '5a68fdf95db93f6477053ddd' })
}
// []
async function getCourses() {
return await Course
.find({ _id: '5a68fdf95db93f6477053ddd' })
}
// []
Any help would be greatly appreciated. Thank you!
EDIT: Showing full find() response.
Upon our discussion as your importing the data from JSON file, So its inserting the _id as string, While finding the document by _id, moognoose automatically converting string to Object.
// Original JSON
{
"_id": "5a68fde3f09ad7646ddec17e",
"tags": [
"aspnet",
"backend"
],
"date": "2018-01-24T21:42:59.605Z",
"name": "ASP.NET MVC Course",
"author": "Mosh",
"isPublished": true,
"price": 15,
"__v": 0
}
Solution 1 :
To insert the _id as in the JSON, change your _id field with $oid as below,
{
"_id": { "$oid": "5a68fde3f09ad7646ddec17e" },
"tags": [
"aspnet",
"backend"
],
"date": "2018-01-24T21:42:59.605Z",
"name": "ASP.NET MVC Course",
"author": "Mosh",
"isPublished": true,
"price": 15,
"__v": 0
}
Solution 2 :
Remove _id from your JSON, MongoDB will generate a _id automatically
{
"tags": [
"aspnet",
"backend"
],
"date": "2018-01-24T21:42:59.605Z",
"name": "ASP.NET MVC Course",
"author": "Mosh",
"isPublished": true,
"price": 15,
"__v": 0
}