Access MongoDB Array of Embedded Documents in PHP - arrays

Hi I'm trying to reach embedded document inside this array:
"_id" : 0,
"name" : "Sykes",
"orders" : [
{
"invoiceNumber" : 788,
"cart" : [
{
"item" : 0,
"pkgId" : 3,
"qty" : 10
}
]
},
{
"invoiceNumber" : 801,
"cart" : [
{
"item" : 1,
"pkgId" : 1,
"qty" : 8
}
]
}
I've tried this:
db.customer.find({_id:1},{'orders.invoiceNumber':1,_id:0}).pretty()
I need to convert this command to PHP. Any help please?

Related

Pushing objects on a specific multidimensional mongoDb collection

i'm fairly new to the mongoDb query language and I'm struggeling with following scenario.
We have a multidimensional dataset that is comprised of:
n users
n projects for each users
n time_entries for each project
What I am trying to achieve is: I would like to push/update a time_entry of a specific project using a collection.update.
Note each pid should be unique for a user
The collection structure I am using looks as follows:
{
"_id" : ObjectId("5d6e33987f8d7f00c063ceff"),
"date" : "2019-01-01",
"users" : [
{
"user_id" : 1,
"projects" : [
{
"pid" : 1,
"time_entries" : [
{
"duration" : 1,
"start" : "2019-08-29T09:54:56+00:00"
}
]
},
{
"pid" : 2,
"time_entries" : []
}
]
},
{
"user_id" : 2,
"projects" : [
{
"pid" : 3,
"time_entries" : []
}
]
}
]
}
I'm currently able to update all projects of a given user using:
"users.$.projects.$[].time_entries"
yet I'm not able to target a specific project, due to the fact the structure contains 2 nesting levels and using multiple $ positional operator is not yet permitted in MongoDb.
"users.$.projects.$.time_entries"
Below is my full query example:
db.times.update(
{ 'users' : { $elemMatch : { 'projects' : { $elemMatch : { 'pid' : 153446871 } } } } },
{ "$push":
{
"users.$.projects.$[].time_entries":
{
"duration" : 5,
"start" : "2019-08-29T09:54:56+00:00"
}
}
}
);
Are there other ways to achieve the same result?
Should I flatten the array so I only use 1 $ positional operator?
Are there other methods to push items on a multidimensional array?
Should this logic be handled on a code level and not a Database level?
You'll need to use the Positional Filtered Operator to achieve that:
db.times.update(
{},
{
$push: {
"users.$[].projects.$[element].time_entries":{
"duration" : 5,
"start" : "2019-08-29T09:54:56+00:00"
}
}
},
{
arrayFilters: [{"element.pid":1}],
multi: true
}
)
This query will push data to the array time_entries for every pid = 1 it finds.
This will give you the result below:
{
"_id" : ObjectId("5d6e33987f8d7f00c063ceff"),
"date" : "2019-01-01",
"users" : [
{
"user_id" : 1,
"projects" : [
{
"pid" : 1,
"time_entries" : [
{
"duration" : 1,
"start" : "2019-08-29T09:54:56+00:00"
},
{
"duration" : 5.0,
"start" : "2019-08-29T09:54:56+00:00"
}
]
},
{
"pid" : 2,
"time_entries" : []
}
]
},
{
"user_id" : 2,
"projects" : [
{
"pid" : 3,
"time_entries" : []
}
]
}
]
}

MongoDB Querying Nested Arrays

I'm having some trouble with querying a Mongo Collection.
I have a Collection like this:
{
"_id" : "555bd34329de3cf232434ef2",
"cars" : [
{
"0" : {
"parts" : [
{
"name" : "x1",
"price" : 12
},
{
"name" : "x2",
"price" : 14
}
]
},
"1" : {
"parts" : [
{
"name" : "y1",
"price" : 8
},
{
"name" : "y2",
"price" : 12
}
]
}
}
]
}
I'd like to return just the following:
"parts" : [
{
"name" : "x1",
"price" : 12
},
{
"name" : "x2",
"price" : 14
}
]
In other words, I need to figure out how to query the Collection by two parameters at the same time:
where the ID matches "555bd34329de3cf232434ef2"
where the "name" of the part matches "x1"
Does anyone know how to do this kind of nested query?
Assuming a document structure like this:
{
"_id" : ObjectId("555bd34329de3cf232434ef2"),
"cars" : [
{
"parts" : [
{
"name" : "x1",
"price" : 12
},
{
"name" : "x2",
"price" : 14
}
]
},
{
"parts" : [
{
"name" : "y1",
"price" : 8
},
{
"name" : "y2",
"price" : 12
}
]
}
]
}
you can run the following query:
db.collection.find({ "_id": ObjectId("555bd34329de3cf232434ef2"), "cars.parts.name" : "x1" }, { "_id": 0, "cars.$": 1 })
which will get you pretty close to where you want to be:
{
"cars" : [
{
"parts" : [
{
"name" : "x1",
"price" : 12
},
{
"name" : "x2",
"price" : 14
}
]
}
]
}
You could get closer using the aggregation framework if that's not good enough...

Sort in mongoDB using a specific order

I am currently looking to sort the sub document, Clients, in a particular order based on an array.
The mongoDB structure is
{
"_id" : "1033",
"Name" : "Test",
"Clients" : [
{
"Id" : 1033,
"Types" : [
{
"Class" : "C",
"Service" : null
},
{
"Class" : "B",
"Service" : null
}
]
},
{
"Id" : 156136,
"Types" : [
{
"Class" : "A",
"Service" : null
},
{
"Class" : "B",
"Service" : null
},
{
"Class" : "C",
"Service" : null
},
{
"Class" : "D",
"Service" : null
}
]
}
]
}
I need the above document displayed in the order based on a array like [B, A, D, C]
So that the output would be as below:
{
"_id" : "1033",
"Name" : "Test",
"Clients" : [
{
"Id" : 1033,
"Types" : [
{
"Class" : "B",
"Service" : null
},
{
"Class" : "C",
"Service" : null
}
]
},
{
"Id" : 156136,
"Types" : [
{
"Class" : "B",
"Service" : null
},
{
"Class" : "A",
"Service" : null
},
{
"Class" : "D",
"Service" : null
},
{
"Class" : "C",
"Service" : null
}
]
}
]
}
Could you please help me on how to achieve this?
I am currently using MongoDB Driver for .Net
Custom ordering is possible via aggregation as specified here.
I've solved my problem with custom sorting , using $addFields and $indexOfArray aggregations.
let imagine that my document has two fields: BouqetId, Name
[
{ "BouqetId" : 2, "Name" : "Name2"},
{ "BouqetId" : 16, "Name" : "Name16"},
{ "BouqetId" : 25, "Name" : "Name25"},
{ "BouqetId" : 15, "Name" : "Name15"},
{ "BouqetId" : 125, "Name" : "Name125"},
{ "BouqetId" : 258, "Name" : "Name258"},
{ "BouqetId" : 127, "Name" : "Name127"}
...
]
and I want to search for Bouqet with Ids [258,15,2,16] and get them with this order.
1/ I filter my collection using $in operator in $match aggregation to get the required documents: view step one in code.
2/ I add a field using $addFields aggregation named Order and assign to it the index of the BouqetId in the searched array using $indexOfArray aggregation.
3/ finally I sort them using the newly added Order field.
4/ I get my custom-ordered result. I can remove the order field but it is ok for now.
here and exmaple in Nodejs :
var db = client.db("MyDatabase");
var collection = db.collection("Bouqets");
var pipeline = [
{
"$match": {
"BouqetId": {
"$in": [
258,
15,
2,
16
]
}
}
},
{
"$addFields": {
"Order": {
"$indexOfArray": [
[
258,
15,
2,
16
],
"$BouqetId"
]
}
}
},
{
"$sort": {
"Order": 1.0
}
}
];
var cursor = collection.aggregate(pipeline);
Here my result :
[
{ "BouqetId" : 258, "Name" : "Name258" , "Order" : 0},
{ "BouqetId" : 15, "Name" : "Name15", "Order" : 1},
{ "BouqetId" : 2, "Name" : "Name2", "Order" : 2},
{ "BouqetId" : 16, "Name" : "Name16", "Order" : 3}
]
here the same example in c# :
IMongoClient client = new MongoClient("mongodb://host:port/");
IMongoDatabase database = client.GetDatabase("MyDatabase");
IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("Bouqets");
var options = new AggregateOptions()
{
AllowDiskUse = false
};
// my array
var searchArray = new int[] {258, 15, 2, 16};
PipelineDefinition<BsonDocument, BsonDocument> pipeline = new BsonDocument[]
{
new BsonDocument("$match", new BsonDocument()
.Add("BouqetId", new BsonDocument()
.Add("$in", new BsonArray(searchArray)
)
)),
new BsonDocument("$addFields", new BsonDocument()
.Add("Order", new BsonDocument()
.Add("$indexOfArray", new BsonArray()
.Add(new BsonArray(searchArray)
)
.Add("$BouqetId")
)
)),
new BsonDocument("$sort", new BsonDocument()
.Add("Order", 1.0))
};
var result = collection.Aggregate(pipeline, options).ToList();
Custom ordering is not possible in MongoDB. You can either sort ascending or descending.

How to retrieve distinct keys inside an object in MongoDB

I have this in MongoDB:
{ "_id" : ObjectId("58fb35531eb5df245d5d434f"), "name" : "d1.html", "indexation" : { "Citroen" : 1, "color" : 1, "Marca" : 1, "rojo" : 1 } }
{ "_id" : ObjectId("58fb35531eb5df245d5d4350"), "name" : "d2.html", "indexation" : { "blancos" : 1, "ocasión" : 1, "Madrid" : 1, "Coches" : 1, "rojo" : 1, "coches" : 1 } }
{ "_id" : ObjectId("58fb35531eb5df245d5d4351"), "name" : "d3.html", "indexation" : { "rojos" : 1, "Ocasión" : 1, "marcas" : 1, "Madrid" : 1, "blancas" : 1, "autos" : 1, "de" : 1 } }
You can see an image containing the above:
And I would like to get the distinct keys inside the object "indexation" in each document.
The result I woul like to get is: ["Citroen", "color", "Marca", "rojo", "blancos", "ocasión", "Madrid", "Coches", "coches", "rojos", "Ocasión", "marcas", "blancas" "autos", "de"].
I'm trying with distinct ("indexation") but I get the whole indexation...
Could you explain to me what do I have to do to get what I want, please?
You can use new $objectToArrray in 3.4.4 version to convert all key & value pair into document arrays followed by $unwind & $group with $addToSet to get distinct keys
db.collection.aggregate([{$project: {indexation: {$objectToArray: "$indexation"}}}, {$unwind:"$indexation"}, {$group:{_id:null, keys:{$addToSet:"$indexation.k"}}}])
For lower version you've to update indexation to look like below and and use
db.collection.distinct("indexation.k")
{ "_id" : ObjectId("58fb35531eb5df245d5d434f"), "name" : "d1.html", "indexation" : [{ "k" : "Citroen", "v" : 1 }, { "k" : "Marca", "v" : 1 }]}

Update / create subdocument in array

I got a data like this in my User collection on my MongoDB database
{
"_id" : ObjectId("5890a9598c36d45435d521c7"),
"name" : "Test Testsson",
"email": "test#test.com,"
"tasks": [{
"_id" : ObjectId("5890a9598c36d45435d521ce"),
"sessionid" : 0,
"value" : 7,
"actions" : [
{
"taskid" : 17,
"time" : 0.85302734375,
"_id" : ObjectId("5890a9598c36d45435d521d6")
},
{
"taskid" : 5,
"time" : 1.39321899414063,
"_id" : ObjectId("5890a9598c36d45435d521d5")
}
]
}, {
"_id" : ObjectId("5890a9598c36d45435d521qw"),
"sessionid" : 1,
"value" : 7,
"actions" : [
{
"taskid" : 1,
"time" : 0.85302734375,
"_id" : ObjectId("5890a9598c36d45435d521zx")
},
{
"taskid" : 5,
"time" : 1.39321899414063,
"_id" : ObjectId("5890a9598c36d45435d521yt")
}
]
}
]
}
I post data to my node / mongoose application and I want to update the tasks array.
I do a POST to /api/user/task/5890a9598c36d45435d521c7
with Body:
{
"sessionid" : 2,
"value" : 12,
"actions" : [
{
"taskid" : 4,
"time" : 0.85302734375,
"_id" : ObjectId("5890a9598c36d45435d521zx")
},
{
"taskid" : 9,
"time" : 1.39321899414063,
"_id" : ObjectId("5890a9598c36d45435d521yt")
}
]
}
I can find my tasks with this query:
User.findOne({
_id: ObjectId('58909be40c50e2d0345e916e'),
"tasks.sessionid": 0},
function(...){/* blah blah */});
But how do I update the data in the tasks array?
If the sessionid exists I want to update the subdocument with the
request body (given that the User exists)
If the sessionid DONT exist I want to create a new object from the
request body in the tasks array (given that the User exists)
If the ObjectId of the User in the url parameter DONT exist send http
status: 500
I tried with update and findAndModify but cant get it to work as expected. Anyone got any idees how to do this.

Resources