Mongodb sorting issue - mongodb

My mongodb collection:
[{
"_id" : ObjectId("5dd6598d55396f36052e347d"),
"isActive" : true,
"myarray" : [
{
"my_id" : "5d967d08821b4031a197b002",
"name" : "jack"
},
{
"my_id" : "5d967d2c821b4031a197b003",
"name" : "manison"
}
]
},
{
"_id" : ObjectId("5dd6598d55396f36052e347d"),
"isActive" : true,
"myarray" : [
{
"my_id" : "5d967d08821b4031a197b002",
"name" : "penelope"
},
{
"my_id" : "5d967d2c821b4031a197b003",
"name" : "cruz"
}
]
}]
Here i am trying to sort based on the name.
not expecting to sort inside the array but expecting outside.
Expecting result be like
[{
"_id" : ObjectId("5dd6598d55396f36052e347d"),
"isActive" : true,
"myarray" : [
{
"my_id" : "5d967d08821b4031a197b002",
"name" : "penelope"
},
{
"my_id" : "5d967d2c821b4031a197b003",
"name" : "cruz"
}
]
},{
"_id" : ObjectId("5dd6598d55396f36052e347d"),
"isActive" : true,
"myarray" : [
{
"my_id" : "5d967d08821b4031a197b002",
"name" : "jack"
},
{
"my_id" : "5d967d2c821b4031a197b003",
"name" : "manison"
}
]
}]
"name" : "cruz" coming first because alphabatically C comes fast than J AND M (which is in second json)
And Prenelop and cruz didn't switched just the main document json switched as per the name order
Query i am using
db.traffic.aggregate([
{$unwind: "$customFieldArray"},
{$sort: {"customFieldArray.field_value":1}},
{$group: {_id:"$_id", customFieldArray: {$push:"$customFieldArray"}}}
]);
But it is sorting inside like taking cruz to penelope and vice versa.
And main json staying stable.
Please have a look

You can do with simple find query with sort cursor
db.traffic.find({}).sort({ "myarray.name": -1 })
From the docs
With arrays, a less-than comparison or an ascending sort compares the
smallest element of arrays, and a greater-than comparison or a
descending sort compares the largest element of the arrays.

Related

Find 2 or more criteria in the same elements of an array in MongoDB

I have a json data structure as follow:
"_id" : {
Inst_Id: 1119689706
},
"items" : [
{
"Token" : "Let",
"Lemma" : "let",
"POS" : "VERB"
},
{
"Token" : "'s",
"Lemma" : "-PRON-",
"POS" : "PRON"
},
{
"Token" : "face",
"Lemma" : "face",
"POS" : "VERB"
},
{
"Token" : "it",
"Lemma" : "-PRON-",
"POS" : "PRON",
}
]
My items are basically fields which have arrays of token of sentences (e.g. "Let's face it inside.) How can I search for 2 or more criteria inside the same item of an array? I have tried $elemMatch but it only matches elements across arrays and not inside one array. For example, I want to look for a sentence for which the token is "face" AND the POS is "VERB" at the same time.
$elemMatch is the way :
db['01'].find(
{items:{$elemMatch:{Token:"face",POS:"VERB"}}}
)
will return whole document. To return only matching array elements, add the same to projection part of query :
db['01'].find(
{items:{$elemMatch:{Token:"face",POS:"VERB"}}},
{items:{$elemMatch:{Token:"face",POS:"VERB"}}}
)
will return
{
"_id" : {
"Inst_Id" : 1119689706.0
},
"items" : [
{
"Token" : "face",
"Lemma" : "face",
"POS" : "VERB"
}
]
}
According to description as mentioned into above question, as a solution to it,please try executing following MongoDB query
db.getCollection("test").find({
items: {
$elemMatch: {
Token: 'face',
"POS": "VERB"
}
}
}, {
'items.$': 1
})

Getting array of object with limit and offset doesn't work using mongodb

First let me say that I am new to mongodb. I am trying to get the data from the collection
Here is the document in my collection student:
{
"_id" : ObjectId("5979e0473f00003717a9bd62"),
"id" : "l_7c0e37b9-132e-4054-adbf-649dbc29f43d",
"name" : "Raj",
"class" : "10th",
"assignments" : [
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc571",
"name" : "1"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc572",
"name" : "2"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc573",
"name" : "3"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc574",
"name" : "4"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc575",
"name" : "5"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc576",
"name" : "6"
}
]
}
the output which i require is
{
"assignments" : [
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc571",
"name" : "1"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc572",
"name" : "2"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc573",
"name" : "3"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc574",
"name" : "4"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc575",
"name" : "5"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc576",
"name" : "6"
}
]
}
for this response i used the following query
db.getCollection('student').find({},{"assignments":1})
Now what exactly I am trying is to apply limit and offset for the comments list I tried with $slice:[0,3] but it gives me whole document with sliced result
but not assignments alone so how can I combine these two in order to get only assignments with limit and offset.
You'll need to aggregate rather than find because aggregate allows you to project+slice.
Given the document from your question, the following command ...
db.getCollection('student').aggregate([
// project on assignments and apply a slice to the projection
{$project: {assignments: {$slice: ['$assignments', 2, 5]}}}
])
... returns:
{
"_id" : ObjectId("5979e0473f00003717a9bd62"),
"assignments" : [
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc573",
"name" : "3"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc574",
"name" : "4"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc575",
"name" : "5"
},
{
"id" : "v_539f65c2-9f45-4d92-b05e-973cf08cc576",
"name" : "6"
}
]
}
This represents the assignments array (and only the assignments array) with a slice from element 2 to 5. You can change the slice arguments (2, 5 in the above example) to apply your own offset and limit (where the first argument is the offset and the limit is the difference between the first and second arguments).
If you want to add a match condition (to address specific documents) to the above then you'd do something like this:
db.getCollection('other').aggregate([
/// match a specific document
{$match: {"_id": ObjectId("5979e0473f00003717a9bd62")}},
// project on assignments and apply a slice to the projection
{$project: {assignments: {$slice: ['$assignments', 2, 5]}}}
])
More details on the match step here.

Query inner value in mongoDB

all
I'm trying to do a join in MongoDB but also, I need to check for conditions and to do a sum on inner values of what comes back from the join.
I will explain.
Currently I have this simple join query which looks like this:
db.Sets.aggregate([
{
$lookup:
{
from: "ExecutionTasks",
localField: "identifier",
foreignField: "setIdentifier",
as: "execTask"
}
}
])
It returns the following results:
/* 1 */
{
"_id" : 1,
"name" : "Demo Set",
"identifier" : "demo-set",
"description" : "Demo Set",
"creator" : {
"id" : 1,
"name" : "admin"
},
"createdDate" : ISODate("2017-03-24T20:09:55.120Z"),
"updatedDate" : ISODate("2017-03-24T20:09:55.120Z"),
"execTask" : [
{
"_id" : 1,
"isActive" : 1,
"type" : "count",
"threshold" : {
"default" : "0",
"deviations" : []
},
"name" : "amishay",
"setIdentifier" : "demo-set",
"description" : "a",
"query" : {
"source" : 1,
"text" : "select * from t"
},
"creator" : {
"id" : 1,
"name" : "admin"
},
"createdDate" : ISODate("2017-03-27T20:03:22.275Z"),
"updatedDate" : ISODate("2017-03-27T20:03:22.275Z")
},
{
"_id" : 2,
"isActive" : 0,
"type" : "count",
"threshold" : {
"default" : "0",
"deviations" : []
},
"name" : "amishay2",
"setIdentifier" : "demo-set",
"description" : "test",
"query" : {
"source" : 1,
"text" : "select * from t"
},
"creator" : {
"id" : 1,
"name" : "admin"
},
"createdDate" : ISODate("2017-03-27T20:03:57.248Z"),
"updatedDate" : ISODate("2017-03-27T20:03:57.248Z")
}
]
}
What I would like to do is to return only the length of the array (execTask) and also only those with the attribute isActive which equals to 1.
So basically I want to get something like:
{
"_id" : 1,
"name" : "Demo Set",
"identifier" : "demo-set",
"description" : "Demo Set",
"creator" : {
"id" : 1,
"name" : "admin"
},
"createdDate" : ISODate("2017-03-24T20:09:55.120Z"),
"updatedDate" : ISODate("2017-03-24T20:09:55.120Z"),
"execTask" : 1
}
I checked online numerous questions but I only saw examples which query the collection attribute and not the joined collection attribute.
Thanks!
You can add $addFields stage after $lookup. The below stage will $filter and calculate the $size for query criteria.
$filter operator is to used to filter the execTask array contents in-place on the mentioned criteria.
Expressions $ and $$ to reference the fields / aggregation operators / aggregation stages and inner variables respectively.
$size operator to calculate the length of filtered array.
$addFields overwrites the existing field execTask to replace its value with the calculated size.
{
$addFields: {
"execTask": {
$size: {
$filter: {
input: "$execTask",
as: "result",
cond: {
$eq: ["$$result.isActive", 1]
}
}
}
}
}
}

MongoDB: query nested array by more than one condition

Say this is one item from my users collection:
{
"_id" : "5545f4c4d0dd52c355a99fbe",
"name" : "Dollie James",
"device" : "iOS",
"gender" : "Female",
"wallet" : [
{
"store" : "All Saints",
"balance" : "$196.11",
"date" : "2014-02-22T22:09:38 -10:00",
"tags" : [
"Tshirt",
"Summer"
]
},
{
"store" : "Nike",
"balance" : "$367.76",
"date" : "2014-04-18T14:44:30 -10:00",
"tags" : [
"Shoes"
]
}
]
}
This record was returned from the following query:
db.users.findOne({$and:[{"wallet.tags" : "Tshirt"}, {"wallet.store":"Nike"}]})
However this is not the result I want because Tshirt and Nike are not in the same object in the wallet array. It seems that mongo is doing a query on the entire array. How can I target this query to only return my two conditions that are in the same object within the array?
You don't need $and (as it will apply conditions independently) but $elemMatch. From the doc:
The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.
In your specific case:
> db.wallet.find(
{wallet: { $elemMatch:{"tags" : "Tshirt", "store":"Nike"}}}
).pretty()
{
"_id" : "5545f4c4d0dd52c355a99f00",
"name" : "Working example",
"device" : "iOS",
"gender" : "Female",
"wallet" : [
{
"store" : "Nike",
"balance" : "$196.11",
"date" : "2014-02-22T22:09:38 -10:00",
"tags" : [
"Tshirt",
"Summer"
]
}
]
}

MongoDB MapReduce--is there an Aggregation alternative?

I've got a collection with documents using a schema something like this (some members redacted):
{
"_id" : ObjectId("539f41a95d1887b57ab78bea"),
"answers" : {
"ratings" : {
"positivity" : [
2,
3,
5
],
"activity" : [
4,
4,
3
],
},
"media" : [
ObjectId("537ea185df872bb71e4df270"),
ObjectId("537ea185df872bb71e4df275"),
ObjectId("537ea185df872bb71e4df272")
]
}
In this schema, the first, second, and third positivity ratings correspond to the first, second, and third entries in the media array, respectively. The same is true for the activity ratings. I need to calculate statistics for the positivity and activity ratings with respect to their associated media objects across all documents in the collection. Right now, I'm doing this with MapReduce. I'd like to, however, accomplish this with the Aggregation Pipeline.
Ideally, I'd like to $unwind the media, answers.ratings.positivity, and answers.ratings.activity arrays simultaneously so that I end up with, for example, the following three documents based on the previous example:
[
{
"_id" : ObjectId("539f41a95d1887b57ab78bea"),
"answers" : {
"ratings" : {
"positivity" : 2,
"activity" : 4
}
},
"media" : ObjectId("537ea185df872bb71e4df270")
},
{
"_id" : ObjectId("539f41a95d1887b57ab78bea"),
"answers" : {
"ratings" : {
"positivity" : 3
"activity" : 4
}
},
"media" : ObjectId("537ea185df872bb71e4df275")
},
{
"_id" : ObjectId("539f41a95d1887b57ab78bea"),
"answers" : {
"ratings" : {
"positivity" : 5
"activity" : 3
}
},
"media" : ObjectId("537ea185df872bb71e4df272")
}
]
Is there some way to accomplish this?
The current aggregation framework does not allow you to do this. Being able to unwind multiple arrays that are know to be the same size and creating a document for the ith value of each would be a good feature.
If you want to use the aggregation framework you will need to change your schema a little. For example take the following document schema:
{
"_id" : ObjectId("539f41a95d1887b57ab78bea"),
"answers" : {
"ratings" : {
"positivity" : [
{k:1, v:2},
{k:2, v:3},
{k:3, v:5}
],
"activity" : [
{k:1, v:4},
{k:2, v:4},
{k:3, v:3}
],
}},
"media" : [
{k:1, v:ObjectId("537ea185df872bb71e4df270")},
{k:2, v:ObjectId("537ea185df872bb71e4df275")},
{k:3, v:ObjectId("537ea185df872bb71e4df272")}
]
}
By doing this you are essentially adding the index to the object inside the array. After this it's just a matter of unwinding all the arrays and matching on the key.
db.test.aggregate([{$unwind:"$media"},
{$unwind:"$answers.ratings.positivity"},
{$unwind:"$answers.ratings.activity"},
{$project:{"media":1, "answers.ratings.positivity":1,"answers.ratings.activity":1,
include:{$and:[
{$eq:["$media.k", "$answers.ratings.positivity.k"]},
{$eq:["$media.k", "$answers.ratings.activity.k"]}
]}}
},
{$match:{include:true}}])
And the output is:
[
{
"_id" : ObjectId("539f41a95d1887b57ab78bea"),
"answers" : {
"ratings" : {
"positivity" : {
"k" : 1,
"v" : 2
},
"activity" : {
"k" : 1,
"v" : 4
}
}
},
"media" : {
"k" : 1,
"v" : ObjectId("537ea185df872bb71e4df270")
},
"include" : true
},
{
"_id" : ObjectId("539f41a95d1887b57ab78bea"),
"answers" : {
"ratings" : {
"positivity" : {
"k" : 2,
"v" : 3
},
"activity" : {
"k" : 2,
"v" : 4
}
}
},
"media" : {
"k" : 2,
"v" : ObjectId("537ea185df872bb71e4df275")
},
"include" : true
},
{
"_id" : ObjectId("539f41a95d1887b57ab78bea"),
"answers" : {
"ratings" : {
"positivity" : {
"k" : 3,
"v" : 5
},
"activity" : {
"k" : 3,
"v" : 3
}
}
},
"media" : {
"k" : 3,
"v" : ObjectId("537ea185df872bb71e4df272")
},
"include" : true
}
]
Doing this creates a lot of extra document overhead and may be slower than your current MapReduce implementation. You would need to run tests to check this. The computations required for this will grow in a cubic way based on the size of those three arrays. This should also be kept in mind.