Multiple $and $or in a MongoDb find - mongodb

I have a query I can't figure out. That query returns nothing:
const read = await db.collection("action_traces")
.find(
{ $and: [
{ $or:
[
{"receipt.receiver": "newdexpocket"},
{"act.account": "newdexpocket"}
]
},
{ $and:
[
{"block_time":{ "$gte": "2018-10-02T00:00:00.000Z" } },
{"block_time":{ "$lt": "2018-10-03T00:00:00.000Z" } }
]
}
]
})
Whereas the following request returns part of the results I want:
const read = await db.collection("action_traces")
.find(
{ $and: [
{ $or:
[
{"receipt.receiver": "newdexpocket"},
{"act.account": "newdexpocket"}
]
},
{"block_time":{ "$lt": 2018-10-03T00:00:00.000Z } }
]
})
The issue is simple, how can get rid of the documents that are older than 2018-10-02T00:00:00.000Z? What am I doing wrong in my first request?
Many thanks in advance,

The and between the critieria should be implied, the below query should be equivalent to what you're trying to achieve (if I understood it right). Please test it.
db.collection("action_traces")
.find({ $or:[
{"receipt.receiver": "newdexpocket"},
{"act.account": "newdexpocket"}
],
"block_time":{ "$gte": "2018-10-02T00:00:00.000Z" ,"$lt": "2018-10-03T00:00:00.000Z" }
})

Related

How to update a property of the last object of a list in mongo

I would like to update a property of the last objet stored in a list in mongo. For performance reasons, I can not pop the object from the list, then update the property, and then put the objet back. I can not either change the code design as it does not depend on me. In brief am looking for a way to select the last element of a list.
The closest I came to get it working was to use arrayFilters that I found doing research on the subject (mongodb core ticket: https://jira.mongodb.org/browse/SERVER-27089):
db.getCollection("myCollection")
.updateOne(
{
_id: ObjectId('638f5f7fe881c670052a9d08')
},
{
$set: {"theList.$[i].propertyToUpdate": 'NewValueToAssign'}
},
{
arrayFilters: [{'i.type': 'MyTypeFilter'}]
}
)
I use a filter to only update the objets in theList that have their property type evaluated as MyTypeFilter.
What I am looking for is something like:
db.getCollection("maCollection")
.updateOne(
{
_id: ObjectId('638f5f7fe881c670052a9d08')
},
{
$set: {"theList.$[i].propertyToUpdate": 'NewValueToAssign'}
},
{
arrayFilters: [{'i.index': -1}]
}
)
I also tried using "theList.$last.propertyToUpdate" instead of "theList.$[i].propertyToUpdate" but the path is not recognized (since $last is invalid)
I could not find anything online matching my case.
Thank you for your help, have a great day
You want to be using Mongo's pipelined updates, this allows us to use aggregation operators within the update body.
You do however need to consider edge cases that the previous answer does not. (null list, empty list, and list.length == 1)
Overall it looks like so:
db.collection.update({
_id: ObjectId("638f5f7fe881c670052a9d08")
},
[
{
$set: {
list: {
$concatArrays: [
{
$cond: [
{
$gt: [
{
$size: {
$ifNull: [
"$list",
[]
]
}
},
1
]
},
{
$slice: [
"$list",
0,
{
$subtract: [
{
$size: "$list"
},
1
]
}
]
},
[]
]
},
[
{
$mergeObjects: [
{
$ifNull: [
{
$last: "$list"
},
{}
]
},
{
propertyToUpdate: "NewValueToAssign"
}
]
}
]
]
}
}
}
])
Mongo Playground
One option is to use update with pipeline:
db.collection.update(
{_id: ObjectId("638f5f7fe881c670052a9d08")},
[
{$set: {
theList: {
$concatArrays: [
{$slice: ["$theList", 0, {$subtract: [{$size: "$theList"}, 1]}]},
[{$mergeObjects: [{$last: "$theList"}, {propertyToUpdate: "NewValueToAssign"}]}]
]
}
}}
]
)
See how it works on the playground example

Perform $group and count in mongoDB aggregation

Given that I have a complex grouping requirement, I was wondering what would be the best approach to achieving my desired result.
My data (result of $project stage) would look something like this:
{
_id:$id
status:"available"
inspectionStatus:"done"
state:"completed"
category:"One"
},
{
_id:$id
status:"booked"
inspectionStatus:"none"
state:"active"
category:"Two"
},
.
.
.
I have tried using $facet to create multiple buckets since the grouping I am trying to create are aggregations of $status + $state + $inspection, but the execution time is way unacceptable, taking something around 1639763842 milliseconds.
I can't use use $accumulator because of mongoDB version (although we can always upgrade to 4.4.x) but I am not sure whether using $accumulator would produce a better response time.
The $facet stage is included:
{
"available": [
{"$match":
{$and: [
{"status": "available"},
{"inspectionStatus": "done"}
]}
}
],
"matched": [
{"$match":
{$and: [
{"status": "booked"},
{"state": "booked"}
]
}
}
],
"inIntake": [
{"$match":
{$and: [
{"status": "available"},
{"inspectionStatus": {$ne: "done"}}
]
}
}
],
"active": [
{"$match":
{$and: [
{"status": "booked"},
{"state": "active"}
]
}
}
],
"unreturned":[
{"$match":
{"status": "forceCompleted"}
}
]
}
If you really want to push the logic to the DB, here's a solution -- but you still have to examine the XX field doc by doc:
db.foo.aggregate([
{$addFields: {XX: {$switch: {
branches: [
{ case: {
$and: [{$eq:["$status","available"]},{$eq:["$inspectionStatus","done"]}]
}, then:'AVAILABLE' },
{ case: {
$and: [{$eq:["$status","booked"]},{$eq:["$state","booked"]}]
}, then:'MATCHED' },
{ case: {
$and: [{$eq:["$status","available"]},{$ne:["$inspectionStatus","done"]}]
}, then:'IN_INTAKE' },
{ case: {
$and: [{$eq:["$status","booked"]},{$eq:["$state","active"]}]
}, then:'ACTIVE' },
{ case: {
$eq:["$status","forceCompleted"]
}, then:'UNRETURNED' },
],
default: null
}}
}}
,{$match: {XX: {$ne: null}}}
]);
The end-to-end timing on this is actually a bunch of millis better than simple find() because less material is transferred but of course the DB engine is working a little harder processing the pipeline.

Using $if or $ifNull condition inside a $match that will be executed in Jaspersoft Studio

{
$match: {
$expr: {
$or: [
{$ifNull : [$P{departmentuid},"warduid":{"$eq": {"$oid":$P{warduids} } } ] },
{$ifNull : [$P{warduids},"orderdepartmentuid": {"$eq": {"$oid":$P{departmentuid} } } ] }
]
}
}
},
I am trying to create a query that will be executed from Jaspersoft Studio. Now, the parameter that will be sent to the query is either of the two set parameters, so I created a $match that has a condition that it will only filter one parameter that will be received.
I am not quite sure if im doing the right query for this. Hope someone can help. Thanks!
The parameters that will be recieved from jaspersoft studio is either $P{departmentuid} or ${warduids}
The logic will just be, If the query recieved $P{departmentuid} parameter, It will apply departmentuid only in the whole query but if it received $P{warduid} it will apply warduid only.
Assuming when $P{departmentuid} or ${warduids}, they will be supplemented the value of null.
You could then do the followings:
db.collection.aggregate([
{
$match: {
$expr: {
$or: [
{
$and: [
{$eq: [null, $P{warduids}]},
{$eq: ["$orderdepartmentuid", $P{departmentuid}]}
]
},
{
$and: [
{$eq: [null, $P{departmentuid}]},
{$eq: ["$warduid", $P{warduids}]}
]
}
]
}
}
}
])
Here is the Mongo playground when $P{warduids} is null.
And here is the Mongo playground when $P{departmentuid} is null

how to use $elemMatch on array specifying an upper field as part of the query

I'd like to retrieve for a specific user, his chats with unread messages.
Lets say I have a simplified chat model like that :
{
lastMessageAt: Date,
participants: [
{
user: String(id),
lastReadAt: Date
}
]
}
How can I achieve my query ?
I have tried several thing like with $elemMatch, but lastMessageAt is unknown at this level...
ChatDB.find({
'participants': {
$elemMatch: { user: '12345', lastReadAt: { $lt: '$lastMessageAt' } }
}
}
Thanks in advance for your help ! :)
$elemMatch operator will find those documents in ChatDB collection that have at least 1 element in participants that matches your criteria. Also my research ended with the conslusion that it is not yet possible to access other document field in $elemMatch operator. Anyway, if this is your goal, then you can use this query:
ChatDB.aggregate([
{
$match: {
"participants.user": "12345",
$expr: {
$lt: [
"$participants.lastReadAt",
"$lastMessageAt"
]
}
}
}
])
Mongo playground
If you also want to filter participants that really matched the criteria, then you need to add a projection stage:
ChatDB.aggregate([
{
$match: {
"participants.user": "12345",
$expr: {
$lt: [
"$participants.lastReadAt",
"$lastMessageAt"
]
}
}
},
{
$project: {
participants: {
$filter: {
input: "$participants",
as: "participant",
cond: {
$and: [
{
$eq: [
"$$participant.user",
"12345"
]
},
{
$lt: [
"$$participant.lastReadAt",
"$lastMessageAt"
]
}
]
}
}
}
}
}
])
Mongo playground
I have found the solution witch is to use the aggregator with the $unwind operator.
await ChatDB.aggregate([
{
$unwind: '$participants'
},
{
$match: {
'participants.user': '12345',
$expr: {
$lt: [
'$participants.lastReadAt',
'$lastMessageAt'
]
}
}
}]);
Hope this will be usefull

How to Transform mongodb query using java code

I am new to MongoDB. I wanted to transform this mongo DB query to java code. I have tried a little bit. But I am having an issue. Please, If anybody helps means that will be a great help for me. Thanks in advance.
db.getCollection('test').aggregate(
[{
$match: {
"ids": 9999999,
$or : [{"tags.name":"roja"},{"tags.location":"US"},{"tags.year":2019}]
}
},
{
'$facet': {
metadata: [{ $count: "total" }],
data: [{
$addFields: {
"weight": {
$map: {
input: "$tags",
as: "tagsEl",
in: {
"$add":
[
{$cond: [ { $eq: [ '$$tagsEl.roja', 'roja' ] }, 15, 1 ]} ,
{$cond: [ { $eq: [ '$$tagsEl.location', 'US' ] }, 10, 1 ]},
{$cond: [ { $eq: [ '$$tagsEl.year', 2019 ] }, 5, 1 ]}
]
}
}
}
}
}, { $skip: 0 }, { $limit: 10 }, { '$sort': { 'weight' : -1 } }]
}
}
]
)
You can use Studio3T to directly convert your query into java code, unless you don't get comfortable with the syntax.
https://studio3t.com/knowledge-base/articles/query-code/
//Get database
MongoDatabase db = mongoClient.getDatabase("testdb");
//get collection
MongoCollection<Document> testCol = db.getCollection("test");
//prepare a list of aggregation pipeline stages.
List<Bson> aggs = new ArrayList<>();
//each pipeline stage you wrote is in js. the equivalent java syntax is...
//for {key1:value1,key2:value2}
new Document().append("key1","value1").append("key2","value2")
//for array such as ["abc",1,{key3,value3}]
Arrays.asList("abc",1,new Document("key3","value3"))
//for example , part of your script can be implemented in java as below.
aggs.add(
new Document("$match",//match aggregation pipeline stage
new Document("ids",9999999)
.append("$or",Arrays.asList(
new Document("tags.name","roja"),
new Document("tags.location","US"),
new Document("tags.year",2019)
))
)
);
//you can add as many stages as you need to aggs list.
//execute
MongoCursor<Document> cursor = testCol.aggregate(aggs).iterator();
while(cursor.hasNext()){
Document d = cursor.next();
//do your operation.
}