If condition does not work / not running - javascript

I'm trying to match mongodb object id with if condition:
if (reply[i].data[j].ref == item._id) console.log('match!!!')
The sample of reply & item:
// REPLY
[{
"_id":10,
"data": [
{
"_id":"57f485203858fe43b464ae52",
"type":"product",
"ref":"57f485473858fe43b464ae56",
"name":"KARUNG",
"direction":"in",
"supplier":null,
"information":"PENYESUAIAN JUMLAH",
"qty":200,
"manifest":null,
"timestamp":"2016-10-05T04:44:16.354Z",
"saldo":1200
}
]
}]
// ITEM
{
"_id" : "57f485473858fe43b464ae56",
"name" : "BERAS KC",
"unit" : "SAK",
"qty" : 213,
"weight" : 10
}
I was also trying to convert both to ObjectId using mongojs.ObjectId, but it doesn't work.
The full code snippet: http://pastebin.com/SYTLVWqT

Related

array in array filter - javascript

hello i need help with array , as you can see my data
{
"age" : "18",
"altKategoriler" : [ "Dramalar" ],
"category" : [ "Aksiyon", "Heyecanlı", "Gerilim" ],
"id" : 5240718100,
"img" : "https://i.ibb.co/k8wx5C8/AAAABW9-ZJQOg-MRljz-Zwe30-JZw-Hf4vq-ERHq6-HMva5-ODHln-Ci-OEV6ir-Rcjt88tcnm-QGQCKpr-K9h-Oll-Ln-Sbb-EI.jpg",
"izlenilmeSayisi" : 0,
"logo" : "https://i.ibb.co/Rb2SrcB/AAAABfcrhh-Rni-Ok-Ct2l-Rys-ZYk-Oi-T0-XTeagkrw-Mkm-U0h-Lr-WIQZHEHg-VXihf-OWCwz-Vv-Qd7u-Ffn-DFZEX2-Ob.webp",
"oyuncuKadrosu" : [ "Diego Luna", "Michael Pena", "Scoot McNairy", "Tenoch Huerta", "Joaquin Cosio" ],
"senarist" : [ "Doug Miro" ],
"time" : "3 Sezon",
"title" : "Narcos: Mexico",
"type" : "Dizi",
"videoDescription" : "Guadalajara Karteli'nin yükselişinin gerçek öyküsünü anlatan bu yeni ve cesur Narcos hikâyesinde, Meksika'daki uyuşturucu savaşının 1980'lerdeki doğuşuna tanıklık edin.",
"videoQuality" : "HD",
"videosrc" : "https://tr.vid.web.acsta.net/uk/medias/nmedia/90/18/10/18/19/19550785_hd_013.mp4",
"year" : "2021",
"yonetmen" : [ "Carlo Bernard", "Chris Brancato" ]
}
I can access elements such as id , title or logo because they are not arrays.
How can I loop through the data inside the array since there is an array in the category in yield?
var data = this.database.filter((item) => item.type == searchType)
var data = this.database.filter((item) => item.category == searchCategory)
It's okay because my type value doesn't have an array.
But when I enter my category value, it only gets the first index[0]. It does not look at other indexes.
in summary,
item.category[0] , item.category[1] , item.category[2]...........
How can I get index browsing like
if your data looks like this :
let data ={
"age" : "18",
"altKategoriler" : [ "Dramalar" ],
"category" : [ "Aksiyon", "Heyecanlı", "Gerilim" ],
"id" : 5240718100,
"img" : "https://i.ibb.co/k8wx5C8/AAAABW9-ZJQOg-MRljz-Zwe30-JZw-Hf4vq-ERHq6-HMva5-ODHln-Ci-OEV6ir-Rcjt88tcnm-QGQCKpr-K9h-Oll-Ln-Sbb-EI.jpg",
"izlenilmeSayisi" : 0,
"logo" : "https://i.ibb.co/Rb2SrcB/AAAABfcrhh-Rni-Ok-Ct2l-Rys-ZYk-Oi-T0-XTeagkrw-Mkm-U0h-Lr-WIQZHEHg-VXihf-OWCwz-Vv-Qd7u-Ffn-DFZEX2-Ob.webp",
"oyuncuKadrosu" : [ "Diego Luna", "Michael Pena", "Scoot McNairy", "Tenoch Huerta", "Joaquin Cosio" ],
"senarist" : [ "Doug Miro" ],
"time" : "3 Sezon",
"title" : "Narcos: Mexico",
"type" : "Dizi",
"videoDescription" : "Guadalajara Karteli'nin yükselişinin gerçek öyküsünü anlatan bu yeni ve cesur Narcos hikâyesinde, Meksika'daki uyuşturucu savaşının 1980'lerdeki doğuşuna tanıklık edin.",
"videoQuality" : "HD",
"videosrc" : "https://tr.vid.web.acsta.net/uk/medias/nmedia/90/18/10/18/19/19550785_hd_013.mp4",
"year" : "2021",
"yonetmen" : [ "Carlo Bernard", "Chris Brancato" ]
}
and if we have array of data you can do something like this :
myArray.filter(item=>item.category.indexOf(searchCategory)>=0)
but if you want to explore in object rather than array you can do this :
data.category.indexOf(searchCategory)>=0
You could make this a bit generic, by testing whether the targeted field is an array, using Array.isArray, and then call a filter on each element, and see if any is positive (using .some()). The filter can be function that is provided, so that it can perform a simple match, or apply a regular expression, or anything else.
Instead of testing with Array.isArray you could skip that step and check whether the value has a .some() method. If so, calling it will give the desired outcome, and otherwise (using the .? and ?? operators), the filter should be applied to the value as a whole:
Here is how that looks:
function applyFilter(data, field, filter) {
return data.filter(item => item[field]?.some(filter) ?? filter(item));
}
// Example use:
var data = [{
"category" : [ "Action", "Thriller", "Horror"],
"type" : "Series",
}, {
"category" : [ "Historical", "Romance" ],
"type" : "Theatre",
}];
// Find entries that have a category that looks like "roman*":
var result = applyFilter(data, "category", value => /^roman.*/i.test(value));
console.log(result);
If you are running on an older version of JavaScript, and don't have support for .? or ??, then use:
return data.filter(item => Array.isArray(item[field])
? item[field].some(filter)
: filter(item));

How to pull nested document from the array of documents in mongodb?

I am trying to remove a nested objects array in my document. The scenario is that i am searching for the days an event will be organised for, by using its eventid
const { eventid, typesOfTicketId } = req.params;
const eventDays = await EventDate.find({event: eventid});
Here eventid is passed from params as "5e9c0f0593ab3c058e282bfa". I then want to remove a requested day from the nested objects array. From the above query, I am receiving an array of dates and on each index of array the document is in this format:
[{
"_id" : ObjectId("5ea7f54b8b22480431f1a455"),
"day" : "1588186800",
"typesOfTicket" : [
{
"_id" : ObjectId("5ea7f54b8b22480431f1a456"),
"ticket" : "Adult Tickets",
"noTickets" : 40,
"price" : 50,
"ticketsLeft" : 40
},
{
"_id" : ObjectId("5ea7f54b8b22480431f1a457"),
"ticket" : "Children Tickets",
"noTickets" : 50,
"price" : 30,
"ticketsLeft" : 50
}
],
"event" : ObjectId("5e9c0f0593ab3c058e282bfa"),
"__v" : 0
},
{
"_id" : ObjectId("5ea7f5678b22480431f1a45f"),
"day" : "1588273200",
"typesOfTicket" : [
{
"_id" : ObjectId("5ea7f5678b22480431f1a460"),
"ticket" : "Male Tickets",
"noTickets" : 50,
"price" : 5,
"ticketsLeft" : 50
},
{
"_id" : ObjectId("5ea7f5678b22480431f1a461"),
"ticket" : "Female Tickets",
"noTickets" : 50,
"price" : 5,
"ticketsLeft" : 50
}
],
"event" : ObjectId("5e9c0f0593ab3c058e282bfa"),
"__v" : 0
}]
What i want is to find a way to remove the document in the nested typesOfTicket array, like lets say i want to remove the Object with id: typesOfTicketId. (e.g typesOfTicketId = "5ea7f5678b22480431f1a461"), the female ticket one by passing its ID.
I have already tried this query:
await EventDate.update({event: eventid}, {
$pull: {
typesOfTicket: {
_id: "typesOfTicketIDHERE"
}
}
});
But the above given query is only working if i am removing the first index of eventDays Array, like if i am deleting the ID: "5ea7f54b8b22480431f1a456", then this will work but if i am going for the id's on the second index like "Female tickets"/"5ea7f5678b22480431f1a461", then it is not working.
I found the solution to my problem, the above query did work correctly after just some adjustments
await EventDate.update({event: eventid}, {
$pull: {
typesOfTicket: {
_id: "typesOfTicketIDHERE"
}
}
}, { multi: true });
Just specifying the multi params to true will do the trick.

Return Just An Element From A Mongodb Query (Newbie)

Is there a correct way to return just an element that matches the search query?
For example if I had
{
"_id" : ObjectId("5806d"),
"title" : "vcvc",
"description" : "vcvc",
"lessons" : [
{
"lesson_body" : "Red Ball",
"lesson_title" : "Red Ball",
"_id" : ObjectId("5806s")
},
{
"lesson_body" : "Green Ball",
"lesson_title" : "Green Ball",
"_id" : ObjectId("5806y")
}
],
"__v" : 0
}
How would I be able to get just the following based on id?
{
"lessons" :
{
"lesson_body" : "Red Ball",
"lesson_title" : "Red Ball",
"_id" : ObjectId("5806s")
}
}
Right now I just have the following, but it seems to just timeout
function(id, classLesson, callback){
Class.find({'_id': classLesson._id}, {lessons: {$elemMatch: {_id: id}}})
}
You want to specify a query projection on the query, which specifies the fields to return from the database.
For example:
Class.find(
{ '_id': classLesson._id }, // query filter
{ lessons: 1 } // query projection
)
Is your query supposed to be performing an $elemMath on the lessons._id field? Right now the $elemMatch in your code is placed in the projection portion of the query and is not being used to filter.

Query to Match on nth Document of an Array

I am new to MongoDB and I am doing some exercises on it. In particular I got stuck on this exercise, of which I report here the question:
Given the following structure for document "Restaurant":
{
"_id" : ObjectId("5704adbc2eb7ebe23f582818"),
"address" : {
"building" : "1007",
"coord" : [
-73.856077,
40.848447
],
"street" : "Morris Park Ave",
"zipcode" : "10462"
},
"borough" : "Bronx",
"cuisine" : "Bakery",
"grades" : [
{
"date" : ISODate("2014-03-03T00:00:00Z"),
"grade" : "A",
"score" : 2
},
{
"date" : ISODate("2013-09-11T00:00:00Z"),
"grade" : "A",
"score" : 6
},
{
"date" : ISODate("2013-01-24T00:00:00Z"),
"grade" : "A",
"score" : 10
},
{
"date" : ISODate("2011-11-23T00:00:00Z"),
"grade" : "A",
"score" : 9
},
{
"date" : ISODate("2011-03-10T00:00:00Z"),
"grade" : "B",
"score" : 14
}
],
"name" : "Morris Park Bake Shop",
"restaurant_id" : "30075445"
}
Write a MongoDB query to find the restaurant Id, name and grades for those restaurants where 2nd element of grades array contains a grade of "A" and score 9 on an ISODate "2014-08-11T00:00:00Z".
I wrote this query:
db.restaurants.find(
{
'grades.1': {
'score': 'A',
'grade': 9,
'date' : ISODate("2014-08-11T00:00:00Z")
}
},
{
restaurant_id: 1,
name: 1,
grades: 1
});
which is not working.
The solution provided is the following:
db.restaurants.find(
{ "grades.1.date": ISODate("2014-08-11T00:00:00Z"),
"grades.1.grade":"A" ,
"grades.1.score" : 9
},
{"restaurant_id" : 1,"name":1,"grades":1}
);
My questions are:
is there a way to write the query avoiding to repeat the grades.1 part?
Why is my query wrong, given that grades.1 is a document object?
If it can help answering my question, I am using MongoDB shell version: 3.2.4
EDIT:
I found an answer to question 2 thanks to this question.
In particular I discovered that order matters. Indeed, if I perform the following query, I get a valid result:
db.restaurants.find({'grades.1': {'date': ISODate("2014-08-11T00:00:00Z"), 'grade':'A', score:9}}, {restaurant_id:1, name:1, grades:1})
Note that this query works only because all subdocument's "fields" are specified, and they are specified in the same order.
Not really. But perhaps an explanation of what you "can" do:
db.junk.find({
"grades": {
"$elemMatch": {
"date" : ISODate("2014-03-03T00:00:00Z"),
"grade" : "A",
"score" : 2
}
},
"$where": function() {
var grade = this.grades[0];
return (
grade.date.valueOf() == ISODate("2014-03-03T00:00:00Z").valueOf() &&
grade.grade === "A" &&
grade.score ==== 2
)
}
})
The $elemMatch allows you to shorten a little, but it is not the "nth" element of the array. In order to narrow that further you need to use the $where clause to inspect the "nth" array element to see if all values are a match.
db.junk.aggregate([
{ "$match": {
"grades": {
"$elemMatch": {
"date" : ISODate("2014-03-03T00:00:00Z"),
"grade" : "A",
"score" : 2
}
}
}},
{ "$redact": {
"$cond": {
"if": {
"$let": {
"vars": { "grade": { "$arrayElemAt": [ "$grades", 0 ] } },
"in": {
"$and": [
{ "$eq": [ "$grade.date", ISODate("2014-03-03T00:00:00Z") ] },
{ "$eq": [ "$grade.grade", "A" ] },
{ "$eq": [ "$grade.score", 2 ] }
]
}
}
},
"then": "$$KEEP",
"else": "$$PRUNE"
}
}}
])
You can do the same logic with $redact as well using .aggregate(). It runs a little quicker, but the basic truth should be clear by now.
So using "dot notation" to specify the "nth" position for each element within the array like you have already done is the most efficient and "brief" way to write this. You cannot make it shorter or better.
Your other attempt is looking for a "document" within "grades.1" that matches exactly the document condition you are providing. If for any reason those are not the only fields present, or if they are indeed in "different order" in the stored document, then such a query condition will not be a match.

MongoDB return specific fields from array

I want to limit my query's result to a set of fields. This is one of my documents:
{
"_id" : "WA9QRuiWtGsr4amtT",
"status" : 3,
"data" : [
{
"name" : "0",
"value" : "Text ..."
},
{
"name" : "1",
"value" : "12345678"
},
{
"name" : "2",
"value" : "Text"
},
{
"name" : "4",
"value" : "2"
},
{
"name" : "8",
"value" : true
},
{
"name" : "26",
"value" : true
},
],
"userId" : "7ouEumtudgC2HX4fF",
"updatedAt" : NumberLong(1415903962863)
}
I want to limit the output to the status field as well a the first and third data document.
This is what I tried:
Meteor.publish('cases', function () {
var fields = {
currentStatus: 1,
'data.0': 1,
'data.2': 1
};
return Cases.find({}, { fields: fields });
});
Sadly it doesn't work. Something else I found is $elemMatch but it only returns the first element:
data: {
$elemMatch: {
name: {
$in: ['0', '2']
}
}
},
How can I limit the output to these fields?
To display status and data(unlimited) fields try
cases.find({}, {"status":1, "data":1})
This is simple query, to limit "data" output you will need to work harder :)
Get 1 element by data.name (not by position):
cases.find({}, {status:1, "data": {$elemMatch:{name:"0"}}})
Get 1 element by data.name, but from a list of values:
cases.find({}, {status:1, "data": {$elemMatch:{name:{$in:["0", "1"]}}}})
To get close to your question, you may try redact. That is new in Mongodb 2.6.
Or play with $unwind and .aggregate() in previous editions.
So far, I do not see a way to return array elements based on a position.

Categories

Resources