how to update the first object in the array of quarterFinals that matches some mongoDB condition? - mongodb

I have the document below and I need to iterate through the quarterFinals array and update the first null address property I find to '123'
{
"data": {
"createTournament": {
"_id": "613122d0c7befdeef3b0b571",
"title": "Jogo de truco",
"description": "",
"location": "Batalha na rua",
"type": "Battle",
"players": [],
"status": "PENDING",
"size": 8,
"entryFee": 1,
"prizePool": 20,
"currency": "USD",
"startDate": "2021-09-01",
"endDate": "2021-09-01",
"rounds": {
"quarterFinals": [{
"id": "9fb6f9ca-c06a-4972-b140-0f2425a90707",
"arena": "",
"firstParticipant": {
"address": null,
"battlesWon": 0
},
"secondParticipant": {
"address": null,
"battlesWon": 0
}
}]
}
}
}
}

One way of doing this is
exports.updateData = async (tournamentID,address)=>{
return await db_collection.updateOne({id:tournamentId,rounds:{$elemMatch:{quarterfinals.address:null}}},{$push:{address:address});
};

Related

Find specific field in array by last date and updating it

I have the document below, and I want to update in exams, the last exam with status "started" (according to date), I need to add to "done" another number
{
"_id": ObjectID("5d2a371cec7eaf00119df614"),
"firstname": "Laura",
"lastname": "Warriner",
"image": "2019-07-14t05:25:23.352z_13.jpg",
"exams": [
{
"examId": "Ba6apmRmz",
"name": "general",
"language": "es",
"version": 2,
"testId": "2000",
"status": [
{
"status": "created",
"date": ISODate("2019-09-08T11:49:42.124Z")
},
{
"status": "started",
"date": ISODate("2019-09-08T11:55:09.873Z"),
"done": [1]
},
{
"status": "started",
"date": ISODate("2019-09-09T12:01:57.886Z"),
"done": [2,3]
},
{
"status": "completed",
"date": ISODate("2019-09-09T12:01:57.886Z")
}
]
}
]
}
Thanks in advance

Query where using the name of key object

I have a column in postgresql of type JSON in this column i have this data:
["row1", {
"date": "05/30/2019",
"name": "JOSH",
"discount": "160.00"
}, {
"date": "04/30/2019",
"name": "JOSH",
"discount": "170.00"
}, {
"date": "03/30/2019",
"name": "JOSH",
"discount": "79.00"
}, "row2", {
"date": "05/30/2019",
"name": "MARIO",
"discount": "110.00"
}, {
"date": "04/30/2019",
"name": "MARIO",
"discount": "101.00"
}, {
"date": "03/30/2019",
"name": "MARIO",
"discount": "98.00"
}]
I need extract data in base to key name 'row1' and 'row2' for this example data.
I have already tried the following sentences without success:
select data->>'row1' from discounts;
SELECT *
FROM discounts
WHERE json_arr_record_keys(data->'row1');
SELECT *
FROM discounts
WHERE EXISTS (
SELECT 1
FROM json_array_elements(data->'row1') elem
);
for now always display empty result, i need by example extract 'row1'
{
"date": "05/30/2019",
"name": "JOSH",
"discount": "160.00"
}, {
"date": "04/30/2019",
"name": "JOSH",
"discount": "170.00"
}, {
"date": "03/30/2019",
"name": "JOSH",
"discount": "79.00"
}

MongoDB Aggregation Error Returning wrong result

I have my json object like this
{
"_id": "5c2e811154855c0012308f00",
"__pclass": "QXRzXFByb2plY3RcTW9kZWxcUHJvamVjdA==",
"id": 44328,
"name": "Test project via postman2//2",
"address": "some random address",
"area": null,
"bidDate": null,
"building": {
"name": "Health Care Facilities",
"type": "Dental Clinic"
},
"collaborators": [],
"createdBy": {
"user": {
"id": 7662036,
"name": "Someone Here"
},
"firm": {
"id": 2520967,
"type": "ATS"
}
},
"createdDate": "2019-01-03T21:39:29Z",
"customers": [],
"doneBy": null,
"file": null,
"firm": {
"id": 1,
"name": "MyFirm"
},
"leadSource": {
"name": "dontknow",
"number": "93794497"
},
"location": {
"id": null,
"city": {
"id": 567,
"name": "Bahamas"
},
"country": {
"id": 38,
"name": "Canada"
},
"province": {
"id": 7,
"name": "British Columbia"
}
},
"modifiedBy": null,
"modifiedDate": null,
"projectPhase": {
"id": 1,
"name": "pre-design"
},
"quotes": [{
"id": 19,
"opportunityValues": {
"Key1": 100,
"Key2 Key2": 100,
"Key3 Key3 Key3": 200,
}
}],
"specForecast": [],
"specIds": [],
"tags": [],
"valuation": "something"
}
I am trying to aggregate using this query in MongoDB. My aggregation key is 4 level deep and also contains spaces. On all online examples shows me the aggregation at the first level. Looking to the online codes, I tried to re-iterate the same with my 4th level deep key.
db.mydata.aggregate([
{$match: {"id": 44328 } } ,
{$group: { _id: "$quotes.id",
totalKey2:{ $sum: "$quotes.opportunityValues.Key2 Key2"},
totalKey3:{ $sum: "$quotes.opportunityValues.Key3 Key3 Key3"}
}
}
]);
This should return
_id totalKey2 totalKey3
0 19 100 300
But it is returning
_id totalKey2 totalKey3
0 19 0 0
What am I doing Wrong?
Although it's not recommended to use space in field names in Mongo, it works as expected.
The problem with your query is that "quotes" is an array and you should first unwind it before grouping it.
This works as expected:
db.mydata.aggregate([
{ $match: { "id": 44328 } } ,
{ $unwind: "$quotes" },
{ $group: { _id: "$quotes.id",
totalKey2:{ $sum: "$quotes.opportunityValues.Key2 Key2" },
totalKey3:{ $sum: "$quotes.opportunityValues.Key3 Key3 Key3" } }
}
]);

Not picking Index - when querying Object -> array -> Object in N1QL

Index not picking when using N1QL to match all the customers belonging to a country
Note : the bucket has around 10 lack records, the document is fetching after 50 seconds,
query
select * from `user-prof` WHERE ANY item IN devices.location SATISFIES item.country IN ["US"]
index
CREATE INDEX id_userProfile ON `user-prof` ( ALL ARRAY v.country FOR v IN devices.location END )
Document
[
{
"customers": {
"devices": {
"user": "u1",
"custid": "14CE5534CCE",
"token": "4D5BE85896833148D696A1397C",
"guest": {
"lastActive": null,
"searches": [
]
},
"latt": "6655059908",
"locale": "en-US",
"location": [
{
"city": "Afc",
"country": "IN"
},
{
"city": "Newyork",
"country": "US"
},
{
"city": null,
"country": null
}
],
"long": "4.21787927806",
"notify": {
"Stats": false
},
"tier": null,
"tmz": "EU",
"version": "0.1"
}
}
},
{
"customers": {
"devices": {
"user": "u2",
"custid": "64CE5534CC1E",
"token": "6D5BE85896833148D696A1397C",
"guest": {
"lastActive": null,
"searches": [
]
},
"latt": "6655059908",
"locale": "en-US",
"location": [
{
"city": "Texas",
"country": "US"
},
{
"city": null,
"country": null
}
],
"long": "4.21787927806",
"notify": {
"Stats": false
},
"tier": null,
"tmz": "EU",
"version": "0.1"
}
}
}
You are using Pre 4.6.2 The variable in the ANY clause needs to be same as the variable in the CREATE INDEX (i.e item ,v )
https://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/indexing-arrays.html

Grouping select with partial

Hello i have here this code:
public function getConversations($user, $limit, $page)
{
$offset = ($page - 1) * $limit;
$qb = $this->createQueryBuilder('message');
$qb
->select('message, partial o.{id}, partial userFrom.{id, username, avatar}, partial userTo.{id, username, avatar}, partial availability.{id, timeFrom}')
->leftJoin('message.from', 'userFrom')
->leftJoin('message.to', 'userTo')
->leftJoin('message.order', 'o')
->leftJoin('o.availabilities', 'availability')
->where($qb->expr()->orX(
$qb->expr()->eq('message.from', ':user'),
$qb->expr()->eq('message.to', ':user')
))
->setParameter('user', $user)
->setFirstResult($offset)
->setMaxResults($limit)
->addOrderBy('message.created', Criteria::DESC);
return $qb->getQuery()->getArrayResult();
}
I want to get only last message connected to each order. If i have 2 messages to order it gives me 2 rows but i need only one (the last one). I tried to use GROUP BY but it didn't work well. It wanted me to put all foreign ids to group by and then the result was really bad.
Result for this is:
{
"id": "813fa986-f89b-411d-8324-727e427cbc01",
"isRead": false,
"created": {
"date": "2016-12-12 18:01:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"content": "Hello to you too",
"contentType": "text",
"from": {
"id": "3e1806d6-f47e-48b4-afdb-1081d9555a3f",
"avatar": "",
"username": "User two"
},
"to": {
"id": "52ead9fa-b498-400b-967d-b49886ab3118",
"avatar": "",
"username": "User one"
},
"order": {
"id": "8349e718-405a-4e88-8155-3896e9a89fdf",
"availabilities": [
{
"id": "9ddc07e0-be3d-453c-b12f-0ee31bfce444",
"timeFrom": {
"date": "2016-12-03 12:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
}
},
{
"id": "813fa986-f89b-411d-8324-727e427cbc00",
"isRead": false,
"created": {
"date": "2016-12-12 18:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"content": "Helllooooooo",
"contentType": "text",
"from": {
"id": "52ead9fa-b498-400b-967d-b49886ab3118",
"avatar": "",
"username": "User one"
},
"to": {
"id": "3e1806d6-f47e-48b4-afdb-1081d9555a3f",
"avatar": "",
"username": "User two"
},
"order": {
"id": "8349e718-405a-4e88-8155-3896e9a89fdf",
"availabilities": [
{
"id": "9ddc07e0-be3d-453c-b12f-0ee31bfce444",
"timeFrom": {
"date": "2016-12-03 12:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
}
and should be just like this:
{
"id": "813fa986-f89b-411d-8324-727e427cbc01",
"isRead": false,
"created": {
"date": "2016-12-12 18:01:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"content": "Hello to you too",
"contentType": "text",
"from": {
"id": "3e1806d6-f47e-48b4-afdb-1081d9555a3f",
"avatar": "",
"username": "User two"
},
"to": {
"id": "52ead9fa-b498-400b-967d-b49886ab3118",
"avatar": "",
"username": "User one"
},
"order": {
"id": "8349e718-405a-4e88-8155-3896e9a89fdf",
"availabilities": [
{
"id": "9ddc07e0-be3d-453c-b12f-0ee31bfce444",
"timeFrom": {
"date": "2016-12-03 12:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
}
}