Partial compare of string using Mongodb aggregation - javascript

I've added about 5000 questions to a collection in my MongoDB database. Now I want to arrange the questions together that are just partially similar like 70% to 80% similar and send them to the frontend.
I tried this below pipeline in mongodb aggregate framework. but it groups questions which are 100% similar.
{
'$group': {
'_id': {
'$toLower': '$question'
},
'count': {
'$sum': 1
},
'allData': {
'$push': {
'_id': '$_id',
'question': '$question',
'categoryId': '$categoryId',
'subCategoryId': '$subCategoryId',
'answers': '$answers',
'solution': '$solution',
'createdAt': '$createdAt',
'updatedAt': '$updatedAt'
}
}
}
}, {
'$match': {
'count': {
'$gte': 2
}
}
}
I want to find the partial similar questions like:
Example 1:
Which sentences are true about earthquakes?
Which sentences are true about earth quakes?
:::The above two question should be marked as similar.
Example 2:
In which European city, The Volvo Group’s headquarters located?
In which European city, The Volvo head quarters located?
:::The above two question should be marked as similar as 70 to 80 percent of the question are similar.

Related

Objection insertGraph, insert new and relate or relate to existing rows

So, I'm not super knowledge with MySQL relations, upserting and such. I'm looking for an explanation on how (if?) this is possible to do.
[
{
scheduledAt: '17:55',
league: { name: 'Champions League - Group Stage' }
},
{
scheduled_at: '19:45',
league: { name: 'Champions League - Group Stage' }
},
{
scheduled_at: '19:30',
league: { name: 'Primera B Metropolitana' },
},
{
scheduled_at: '21:00',
league: { name: 'Primera B Metropolitana' }
}
]
Say I wanted to insert this graph of data. The root objects are going into the fixtures table, and the league property is this relation in the Fixtures model.
{
league: {
relation: Model.BelongsToOneRelation,
modelClass: `${__dirname}/League`,
join: {
from: 'fixtures.league_id',
to: 'leagues.id'
}
}
}
So, currently if I use insertGraph to insert all this data. It's inserts into both the fixtures and leagues table and relates as you would expect.
{
"scheduled_at": "17:55",
"league": {
"name": "Champions League - Group Stage",
"created_at": "2018-10-03T13:02:03.995Z",
"id": 1
},
"league_id": 1
"created_at": "2018-10-03T13:02:04.042Z",
"id": 1
}
However if I insert the exact same league object, it will just create another duplicate league and fixture row with the next incremented ID (2 in this case).
Is it possible for it to find if a league exists with that name, and then use that row/ID as the league_id, like so:
{
"scheduled_at": "17.55",
"league_id": 1
"created_at": "2018-10-03T13:02:04.042Z",
"id": 2
}
Sorry if I've explained this horrendously. But I'm not so hot on the terminology so I don't know what I'm actually looking to do. I feel like this is a super easy thing, but maybe my structure or method is wrong.

MongoDB aggregate conditional push with fixed array length

Scenario: Members can choose (yes/no) from 4 different activities available.
Based on the following input,
[
{
name:"member1",
activity:"activity1",
selected:true
},
{
name:"member1",
activity: "activity3",
selected:false
},
{
name:"member2",
activity:"activity2",
selected:true
},
{
name:"member2",
activity: "activity4",
selected:false
}
]
need a result as follows, showing member's choice on all the 4 activities in the order of activity 1 to 4 (including the activities which the user has not made a decision yet)
[
{
name:"member1",
activities:[true,null,false,null]
},
{
name:"member2",
activities:[null,true,null,false]
}
]
I tried the following code,
db.collection("MemberActivities").aggregate(
[
{
$group:
{
_id: "$MemberName",
activities: { $push: "$selected"}
}
}
]
but, it contain only the activities the user has made a decision (yes/no).
[
{
_id:"member1",
activities:[true,false]
},
{
_id:"member2",
activities:[true,false]
} ]
Please guide on how to get desired result.

List most common occurring games between friends

I just started learning mongodb so my pick on the queries is not really good at the moment.
So I will get straight to the problem. Following is my document for every user
{
id:"14198959",
user_name:"kikStart2X"
friends:[
{
friend_id:"1419897878",
friend_name:"nitpick",
profile_picture:"some image data",
},
{
friend_id:"14198848418",
friend_name:"applePie",
profile_picture:"some image data",
}, //etc
],
games:[
{
game_id:"1" ,
game_name:"Bunny Hop"
},
{
game_id:"2" ,
game_name:"Racing cars",
},
],
}
Now the collection has all the documents with same structure
1) friends array represents the users that are my friends
2) games array represents the games that I have played
My friends would have the same document structure with games array containing the games they have played
What I want is to list most common games between me and my friends in ascending/descending or any order.
The result should look like the following
{
result:
[
{
game_id:"1" ,
game_name:"Bunny Hop",
friends:
[
{
friend_id:"1419897878",
friend_name:"nitpick",
profile_picture:"some image data",
},
{
friend_id:"14198848418",
friend_name:"applePie",
profile_picture:"some image data",
},
]
},
{
game_id:"2" ,
game_name:"Racing cars",
friends:
[
{
friend_id:"71615343",
friend_name:"samuel",
profile_picture:"some image data",
},
]
}
]
}
I know this is a bit tough to achieve but I don't know how to do it and have searched the internet for hours.
Thanks in advance to all you MongoDB champs.
You can try below aggregation query.
The query will $unwind friends array followed up with $lookup for each friends games.
Next step is $unwind friendsgames followed by comparison using $setIntersection in $project stage to find the common games between the input document games and each of friendsgames.
Final step is to $group by games to collect friends with same games.
db.collection.aggregate( [
{ $unwind:"$friends" },
{
$lookup: {
from: collectionname,
localField: "friends.friend_id",
foreignField: "id",
as: "friendsgames"
}
},
{ $unwind:"$friendsgames" },
{ $project:{commongames:{$setIntersection:["$games", "$friendsgames.games"]}, friends:1 }},
{ $unwind:"$commongames" },
{ $group:{_id:"$commongames", friends:{$push:"$friends"} } }
] )

Having trouble wrapping my head around complex $group'ing/aggregation

I have a schema that is something like this:
{
_id: <objectid>
customer: <objectid>
employee: <objectid>
date: <Month/day/year>
amount: <Number>
}
Using angular, I'm trying to make a page that pulls that data and builds separate tables for each day. So something like I would have a tab for yesterday, that would open up a view for a table that has all of my employees listed and the sum of their for the day. Something like this:
[{
date: 10/29/2019
dataFromThisDate: [
{
employee: <name>
sumAmount: <sum(amount for this date)>
list: [<array of all of the transaction _ids
},
{
employee: <name 2>
//etc
}]
},
{
date: 10/30/2019
dataFromThisDate: //etc
}]
Basically as far as I've gotten is just:
MyCollection.aggregate(
[{
$group: {
_id: "$date"
}
}],function(err, result) { //blah }
)
But I'm not sure how to even do nested grouping (first by date, then by employee on that date). Just thinking through it, it feels like I would have to group by date, then pass on all the data to a new grouping pipeline?
Sorry I don't have more of what I've tried, this whole aggregation thing is just completely new to me and I can't find good examples that are similar enough to what I'm trying to do to learn from. I looked at the api docs for mongodb and I understand their basic examples and play around with them, but I'm just having a hard time coming up with how to do my more complex example.
You can try something like this. This uses two groups. First group by date and employee, summing the amount and adding the transaction ids. Second group by date and add the employees with their total amount and transactions list.
aggregate([{
$group: {
_id: {
date: "$date",
employee: "$employee"
},
amount: {
$sum: "$amount"
},
transactionIds: {
$push: "$_id"
}
}
}, {
$group: {
_id: "$_id.date",
dataFromThisDate: {
$push: {
employee: "$_id.employee",
sumAmount: "$amount",
list: "$transactionIds"
}
}
}
}])
Output
{
"_id": "12/21/2016",
"dataFromThisDate": [{
"employee": "employee1",
"sumAmount": 100,
"list": [ObjectId("58151e881ac3c9ce82782663")]
}, {
"employee": "employee2",
"sumAmount": 73,
"list": [ObjectId("58151e881ac3c9ce82782665"), ObjectId("58151e881ac3c9ce82782666")]
}]
}

Appending an entry in one to many embedded document mongodb

I am new to MongoDB and I came across the following use case:
Lets say I have my mongodb document like this:
{
_id: "joe",
name: "Joe Bookreader",
numbers: [
{
mobile: 741134217,
},
{
home: 123452411
}
]
}
Now I need to two perform two operations:
Add a new number {office:112342282}
Delete users home number
I believe that we can do this in Mongo, but I am not getting the syntax anywhere, neither I could find it in the MongoDB documentation.
P.S. I am doing this using Monk Library, monk specific syntax would be of great help. But otherwise also it would help me!
What you want is Mongo's $pull and $push operator
You should be able to do it by doing the following:
db.User.update({_id: 'joe'}, {$push: {numbers: {office: 112342282}}, $pull: {numbers: {home: 123452411}}});
Unfortunately, Mongo doesn't let you operate on the same field with both the $push and $pull operators at the same time (see this issue). So it really needs to be:
db.User.update({_id: 'joe'}, { $push: { numbers: { office: 112342282}}})
db.User.update({_id: 'joe'}, { $pull: { numbers: {home: 123452411}}})
Using monk's style:
var users = db.get('users');
users.update({_id: 'joe'}, { $push: { numbers: { office: 112342282}}})
users.update({_id: 'joe'}, { $pull: { numbers: {home: 123452411}}})

Categories