how to show multi array data in my console log - angularjs

i am new in angularjs and mongodb please check below data and code.. i want to show data in console log but its show undefined i don't know why, i was try many times but does not success please guys help me i am very thankful to you please check below code
var current_schedules = this.current.schedule;
if (angular.isArray(current_schedules)) {
for (var j = 0; j < current_schedules.length; j++) {
console.log(current_schedules[j].length);
for (var i = 0; i < current_schedules[j].length; i++) {
console.log(current_schedules[j][i].title);
}
}
}
//MOngodb Data
"schedule" : [
[
{
"_id" : ObjectId("58f76aa9be4d311a78f24f78"),
"sch_end" : ISODate("2017-04-24T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-24T00:00:00.000Z"),
"available" : false,
"title" : "test3"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f77"),
"sch_end" : ISODate("2017-04-25T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-25T00:00:00.000Z"),
"available" : false,
"title" : "test4"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f76"),
"sch_end" : ISODate("2017-04-27T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-27T00:00:00.000Z"),
"available" : false,
"title" : "test6"
}
],
[
{
"_id" : ObjectId("58f76aa9be4d311a78f24f78"),
"sch_end" : ISODate("2017-04-24T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-24T00:00:00.000Z"),
"available" : false,
"title" : "test9"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f77"),
"sch_end" : ISODate("2017-04-25T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-25T00:00:00.000Z"),
"available" : false,
"title" : "test10"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f76"),
"sch_end" : ISODate("2017-04-27T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-27T00:00:00.000Z"),
"available" : false,
"title" : "test11"
}
]
]
when i use console.log(current_schedules); its show data

Related

not able to query through an array in mongodb aggregation

this is the collection "Record" :
{
"_id" : "883f6174-fbbb-47f8-b280-7a798e8c0664",
"history" : [
{
"_id" : "6bdd2919-9fe5-48b8-a009-5dec0efc10c3",
"oldStatus" : "-NA-",
"newStatus" : "IN_PROGRESS",
"createdAt" : ISODate("2022-07-22T20:58:30.361Z")
},
{
"_id" : "eedb3775-93e2-4f0d-afda-0032b7b343dd",
"oldStatus" : "IN_PROGRESS",
"newStatus" : "ACCEPTED",
"createdAt" : ISODate("2022-10-10T16:30:04.781Z")
}
],
"source" : {
"$ref" : "Source",
"$id" : "65268f59-2a92-4414-8a79-c454c8059005"
},
"pipelineCreatedAt" : ISODate("2022-07-22T20:58:30.361Z")
}
I tried
{$addFields: {inProgressDate : {$cond:{if:{$and: [{ $eq: [ "$history.oldStatus", '-NA-' ] },{ $eq: [ "$history.newStatus", 'IN_PROGRESS' ] }]},then:"$history.createdAt",else:null}}}}
Expecting
My goal is :
WHERE progress time = oldStatus : "-NA-" and newStatus : "IN_PROGRESS"
and accepted time = oldStatus : "IN_PROGRESS" and newStatus : "ACCEPTED"

How to make new array from another array during aggregation?

I have following document:
{
"subscriptionIds" : [
ObjectId("60c312c6dbb5a49fbbf560ea")
],
"gps" : {
"type" : "Point",
"coordinates" : [
23.942706,
54.932539
]
},
"online" : false,
"suspended" : false,
"hwModel" : "",
"fw" : "",
"lastSeen" : ISODate("2021-06-16T04:43:36.682Z"),
"lastSimRequest" : ISODate("2021-06-16T04:34:59.749Z"),
"lastLocation" : "LT",
"lastLocationType" : "gps",
"createdAt" : ISODate("2021-05-20T10:37:16.025Z"),
"updatedAt" : ISODate("2021-06-11T07:37:56.981Z"),
"notes" : "",
"psk_seed" : "QTAebOeNP4nIs-JJSNNlkAQ78N_VaxOq98-_lQPCyZQ=",
"lastOnline" : ISODate("2021-06-15T08:01:59.886Z"),
"lastOffline" : ISODate("2021-06-16T04:43:36.682Z"),
"onlineReason" : "deviceOnlineStatusFromAC",
"offlineReason" : "deviceOfflineStatusTimeout",
"allocationSettings" : "dataplan",
"subscriptionDataplans" : [
{
"_id" : ObjectId("5fae82fc1224cc8d62b5bf17"),
"organizationId" : ObjectId("5dd63d1c1d042f3018e8374e"),
"organizationName" : "",
"name" : "Naujas plan Telia 75GB",
"enabled" : true,
"contractsId" : [
ObjectId("5e32847ab8013befcc14bb1b"),
ObjectId("5e32847ab8013befcc14bb1b")
],
"simQuota" : 0,
"periodQuota" : NumberLong(0),
"allocationRules" : null,
"createdAt" : ISODate("2020-11-13T12:58:36.650Z"),
"updatedAt" : ISODate("2021-06-14T08:08:28.728Z"),
"notes" : "",
"allowRoaming" : false,
"enablePriorityOrdering" : false,
"priorityOrdering" : ""
},
{
"_id" : ObjectId("5fcf25662b1c7d9bab8c1f7d"),
"organizationId" : ObjectId("5dd63d1c1d042f3018e8374e"),
"organizationName" : "",
"name" : "London test",
"enabled" : true,
"contractsId" : [
ObjectId("5e5dfea1efcf754767408eae")
],
"simQuota" : 0,
"periodQuota" : NumberLong(0),
"createdAt" : ISODate("2020-12-08T07:04:06.255Z"),
"updatedAt" : ISODate("2021-06-15T09:28:07.472Z"),
"notes" : "",
"allowRoaming" : true,
"enablePriorityOrdering" : false,
"priorityOrdering" : ""
}
],
}
Is there a way to make following array using "_id" and "allowRoaming" fields:
"dataplanRoaming": [
{
"_id" : ObjectId("5fae82fc1224cc8d62b5bf17"),
"allowRoaming" : false,
},
{
"_id" : ObjectId("5fcf25662b1c7d9bab8c1f7d"),
"allowRoaming" : true,
}
]
My best result was, I tried using project, addFields etc still can't get structure which I want. Rest of query works just fine just missing this part
"dataplanRoaming" : [
[
false,
true
],
[
ObjectId("5fae82fc1224cc8d62b5bf17"),
ObjectId("5fcf25662b1c7d9bab8c1f7d")
]
],
I hoped that {$addFields:{dataplanRoaming:["$subscriptionDataplans.allowRoaming", "$subscriptionDataplans._id"]}},
would give me wanted result it just made array with _id and allowRoaming as separates fields?
Is there a way to create my wanted result using aggregation etc?
$map to iterate loop of subscriptionDataplans and return needed feidls
db.collection.aggregate([
{
$addFields: {
dataplanRoaming: {
$map: {
input: "$subscriptionDataplans",
in: {
_id: "$$this._id",
allowRoaming: "$$this.allowRoaming"
}
}
}
}
}
])
Playground

how to get length in angularjs because of length my second loop not run

i have below code in angularjs but when i want length of my array 0 position its show undefined please check below code that's why my second loop does not run. in angular how to solve this type of problem anyone help me please
this.current_engineer = response.data;
var current_schedules = this.current.schedule;
console.log(current_schedules);
console.log(current_schedules.length);
console.log(current_schedules[0].length);
if (angular.isArray(current_schedules)) {
for (var j = 0; j < current_schedules.length; j++) {
for (var i = 0; i < current_schedules[j].length; i++) //this loop does not run because of length how to get length
{
console.log(current_schedules[j][i].title);
}
}
}
///------------Mongodb Database-----------------------
"schedule" : [
[
{
"_id" : ObjectId("58f76aa9be4d311a78f24f78"),
"sch_end" : ISODate("2017-04-21T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-21T00:00:00.000Z"),
"available" : false,
"title" : "test3"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f77"),
"sch_end" : ISODate("2017-04-22T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-22T00:00:00.000Z"),
"available" : false,
"title" : "test4"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f76"),
"sch_end" : ISODate("2017-04-23T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-23T00:00:00.000Z"),
"available" : false,
"title" : "test6"
}
],
[
{
"_id" : ObjectId("58f76aa9be4d311a78f24f78"),
"sch_end" : ISODate("2017-04-24T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-24T00:00:00.000Z"),
"available" : false,
"title" : "test9"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f77"),
"sch_end" : ISODate("2017-04-25T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-25T00:00:00.000Z"),
"available" : false,
"title" : "test10"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f76"),
"sch_end" : ISODate("2017-04-27T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-27T00:00:00.000Z"),
"available" : false,
"title" : "test11"
}
]
],
Your property's value at current_schedules[0] is a js object and js object doesn't have any property called length. You could use something like Object.keys(current_schedules[0]).length.
Note: For IE9+ and all other modern ES5+ capable browsers.

Fetch a field from array MongoDB Meteor

Hi I have the following collection structure:
{
"_id" : "HZw2ktDPm6EWnGaFt",
"createdAt" : ISODate("2017-04-16T17:40:59.055Z"),
"pollName" : "",
"entryOwner" : "eHPeQPMd94MQFNXmg",
"question" : [
{
"name" : "Question 1",
"questionId" : "sdPzbn9SWjE46HtM2"
},
{
"name" : "Question 2",
"questionId" : "vpMrpbJ2LZKMLEYKe"
}
],
"sharedWith" : [
{
"id" : "jjX5EDdqMtcyQwd6h",
"name" : "person 1",
"votes" : 0
},
{
"id" : "b3Ctr6LFZMd9smd4B",
"name" : "person 2",
"votes" : 0
}
],
"voters" : [
{
"voterId" : "eHPeQPMd94MQFNXmg",
"questionId" : "vpMrpbJ2LZKMLEYKe",
"optionId" : "EKnYKXEFBWnr4hnCP",
"peopleId" : "b3Ctr6LFZMd9smd4B"
},
{
"voterId" : "eHPeQPMd94MQFNXmg",
"questionId" : "vpMrpbJ2LZKMLEYKe",
"optionId" : "EKnYKXEFBWnr4hnCP",
"peopleId" : "jjX5EDdqMtcyQwd6h"
},
{
"voterId" : "eHPeQPMd94MQFNXmg",
"questionId" : "sdPzbn9SWjE46HtM2",
"optionId" : "rjYLitibXDJjGYKM7",
"peopleId" : "b3Ctr6LFZMd9smd4B"
},
{
"voterId" : "eHPeQPMd94MQFNXmg",
"questionId" : "Q6JiaGFAi2LRHS7GQ",
"optionId" : "wFoduKp23cSYJJG9i",
"peopleId" : "b3Ctr6LFZMd9smd4B"
}
]
}
I would like to get the value of Voters.peopleId by using these values.
"voterId" : "eHPeQPMd94MQFNXmg",
"questionId" : "vpMrpbJ2LZKMLEYKe",
"optionId" : "EKnYKXEFBWnr4hnCP",
I tried this and it didnt work it returns the whole document but what i want as a return is just one field:
var getPeopleId = Polls.findOne({
_id:this.props.poll._id}, {"voters": {
$elemMatch :{voterId:Meteor.userId(),questionId:selectedQuestionId,optionId:selectedOptionId}}})
Many thanks
You've got $elemMatch in the projection (2nd param) instead of the query (1st param). You also need to project the result to only include the first match. Try:
const poll = Polls.findOne(
{
_id:this.props.poll._id,
voters: {
$elemMatch: {
voterId: Meteor.userId(),
questionId: selectedQuestionId,
optionId: selectedOptionId
}
},{
'voters.$': 1
});
// guard against missing keys or no results
const peopleId = poll && poll.voters && poll.voters.peopleId;

unable to use $pullAll

I have a collection which contains the documents as shown below
{
"_id" : ObjectId("56d92901f9d573cc1c1fb8bb"),
"busEntryExitInformation" : {
"dateTime" : ISODate("2016-03-04T06:19:45.914+0000"),
"busEntryExitEvent" : [
{
"plateNumber" : "ADFN3R2",
"direction" : "EXIT",
"routeNumber" : NumberInt(929),
"driverID" : "DId5",
"driverName" : "john",
"_id" : ObjectId("56d92901f9d573cc1c1fb8c0")
},
{
"plateNumber" : "ADFN3R4",
"direction" : "EXIT",
"routeNumber" : NumberInt(652),
"driverID" : "DId2",
"driverName" : "jack",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bf")
},
{
"plateNumber" : "ADFN3R8",
"direction" : "EXIT",
"routeNumber" : NumberInt(500),
"driverID" : "DId5",
"driverName" : "john",
"_id" : ObjectId("56d92901f9d573cc1c1fb8be")
},
{
"plateNumber" : "ADFN3R7",
"direction" : "ENTRY",
"routeNumber" : NumberInt(500),
"driverID" : "DId3",
"driverName" : "mack",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bd")
},
{
"plateNumber" : "ADFN3R2",
"direction" : "EXIT",
"routeNumber" : NumberInt(652),
"driverID" : "DId2",
"driverName" : "sandesh",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bc")
}
],
"cameraIntrinsics" : {
"cameraFocalLength" : NumberInt(35),
"cameraAngle" : NumberInt(20),
"imageWidth" : "1920",
"imageHeight" : "1080",
"frameRate" : NumberInt(25)
},
"cameraExtrinsics" : {
"cameraId" : NumberInt(1),
"cameraName" : "Under Route-090 NorthboundBridge",
"cameraDirection" : "Towards Northbound Lanes",
"cameraLatitude" : 1.350228,
"cameraLongitude" : 103.984889,
"cameraHeight" : NumberInt(30)
}
},
"__v" : NumberInt(0)
}
}
where busEntryExitEvent is a array i am trying to remove the array elements where direction is exit across all the documents in the collection
result should contain all the documents with direction entry
{
"_id" : ObjectId("56d92901f9d573cc1c1fb8bb"),
"busEntryExitInformation" : {
"dateTime" : ISODate("2016-03-04T06:19:45.914+0000"),
"busEntryExitEvent" : [
{
"plateNumber" : "ADFN3R7",
"direction" : "ENTRY",
"routeNumber" : NumberInt(500),
"driverID" : "DId3",
"driverName" : "mack",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bd")
}
],
"cameraIntrinsics" : {
"cameraFocalLength" : NumberInt(35),
"cameraAngle" : NumberInt(20),
"imageWidth" : "1920",
"imageHeight" : "1080",
"frameRate" : NumberInt(25)
},
"cameraExtrinsics" : {
"cameraId" : NumberInt(1),
"cameraName" : "Under Route-090 NorthboundBridge",
"cameraDirection" : "Towards Northbound Lanes",
"cameraLatitude" : 1.350228,
"cameraLongitude" : 103.984889,
"cameraHeight" : NumberInt(30)
}
},
"__v" : NumberInt(0)
}
}
i am trying to do like this
db.busEntryExitDoc.update(
{ $pull: { busEntryExitEvent: { "direction" : "EXIT"} } },
{ multi: false }
)
this has to be done to all the documents there is no where condition here how to do it please help
The MongoDB update() methods expects three arguments: db.collection.update(query, update, options). If you want to perform the update on all documents, just use an empty object as query:
db.busEntryExitDoc.update({},
{ $pull: { 'busEntryExitInformation.busEntryExitEvent' : {'direction' : "EXIT"} } },
{ multi: true }
);
Also, note the use of the dot notation to access the array field: you have to point the $pull operator to an array otherwise the operation fails. Finally, use { multi: true } as options since you want to update all the documents in your collection.
You are doing it wrong the first argument to .update() is the "query" argument which should be an empty document here.
db.busEntryExitDoc.update(
{},
{ $pull: { "busEntryExitInformation.busEntryExitEvent": { "direction": "EXIT" } } },
{ multi: true }
)
The same thing apply to the .updateMany() method
db.busEntryExitDoc.update(
{},
{ $pull: { "busEntryExitInformation.busEntryExitEvent": { "direction": "EXIT" } } },
)

Resources