Doctrine ODM Query Builder Subquery - mongodb

I'm using an Akeneo 1.4 system with mongoDB and have several associations for the data. Now I want to query for products with an associationType which has a specific product in it. The mongo data looks like following in the database:
"associations": [
{
"_id": ObjectId("565867d7c6e41f4408d0068f"),
"associationType": 5,
"groupIds": [
],
"owner": {
"$ref": "pim_catalog_product",
"$id": ObjectId("56560373c6e41f5b688b47d7"),
"$db": "akeneo_pim"
},
"products": [
{
"$ref": "pim_catalog_product",
"$id": ObjectId("56560372c6e41f5b688b4583"),
"$db": "akeneo_pim"
}
]
},
{
"_id": ObjectId("565867d7c6e41f4408d00690"),
"associationType": 6,
"groupIds": [
],
"owner": {
"$ref": "pim_catalog_product",
"$id": ObjectId("56560373c6e41f5b688b47d7"),
"$db": "akeneo_pim"
},
"products": [
]
}
]
I know how to query for the products array with in:
$queryBuilder->addOr(
$queryBuilder->expr()->field('associations.products.$id')->in(array(new \MongoId($product->getId())))
);
But I donĀ“t know how to query only for products with a specific associationType (e.g. 5) AND with the given productId. Can I do something like a subquery in Doctrine ODM? I tried already with multiple QueryBuilder objects but that didn't work.

Related

Loopback 3 query by Property of a embedded model

I'm using loopback 3 to build a backend with mongoDB.
So i have 2 models: Object and Attachment. Object have a relation Embeds2Many to Attachment.
Objects look like that in mongoDB
[
{
"fieldA": "valueA1",
"attachments": [
{
"id": 1,
"url": "abc.com/image1"
},
{
"id": 2,
"url": "abc.com/image2"
}
]
},
{
"fieldA": "valueA2",
"attachments": [
{
"id": 4,
"url": "abc.com/image4"
},
{
"id": 5,
"url": "abc.com/image5"
}
]
}
]
The question is: how can i get Objects with attachments.id=4 over the RestAPI?
I have try with the where and include filter. But it didn't work. It look like, that this function is not implemented in loopback3, right?
I have found the solution. It only works on Mongodb, Cloudant and Memory database.
{
"filter": {
"where": {
"attachments.id": 4
}
}
}

Delete sub-document from array in array of sub documents

Let's imagine a mongo collection of - let's say magazines. For some reason, we've ended up storing each issue of the magazine as a separate document. Each article is a subdocument inside an Articles-array, and the authors of each article is represented as a subdocument inside the Writers-array on the Article-subdocument. Only the name and email of the author is stored inside the article, but there is an Writers-array on the magazine level containing more information about each author.
{
"Title": "The Magazine",
"Articles": [
{
"Title": "Mongo Queries 101",
"Summary": ".....",
"Writers": [
{
"Name": "tom",
"Email": "tom#example.com"
},
{
"Name": "anna",
"Email": "anna#example.com"
}
]
},
{
"Title": "Why not SQL instead?",
"Summary": ".....",
"Writers": [
{
"Name": "mike",
"Email": "mike#example.com"
},
{
"Name": "anna",
"Email": "anna#example.com"
}
]
}
],
"Writers": [
{
"Name": "tom",
"Email": "tom#example.com",
"Web": "tom.example.com"
},
{
"Name": "mike",
"Email": "mike#example.com",
"Web": "mike.example.com"
},
{
"Name": "anna",
"Email": "anna#example.com",
"Web": "anna.example.com"
}
]
}
How can one author be completely removed from a magazines?
Finding magazines where the unwanted author exist is quite easy. The problem is pulling the author out of all the sub documents.
MongoDB 3.6 introduces some new placeholder operators, $[] and $[<identity>], and I suspect these could be used with either $pull or $pullAll, but so far, I haven't had any success.
Is it possible to do this in one go? Or at least no more than two? One query for removing the author from all the articles, and one for removing the biography from the magazine?
You can try below query.
db.col.update(
{},
{"$pull":{
"Articles.$[].Writers":{"Name": "tom","Email": "tom#example.com"},
"Writers":{"Name": "tom","Email": "tom#example.com"}
}},
{"multi":true}
);

What is the element query in Mongodb to get value?

I am working on mongodb Aggregation query but i don't know how to get the array value from the JSON by joining two collection, please go through my below sample JSON.
COKE Collection
[{
"name": "Mirinda",
"_id": "894567894769476948"
}, {
"name": "COKE",
"_id": "857856856879694769"
}, {
"name": "PEPSI",
"_id": "785485686846684684"
}]
COKE Order Collection
{
"_id": "0908080808031338013101",
"drink": [{
"name": "Mirinda",
"_id": "894567894769476948"
}, {
"name": "COKE",
"_id": "857856856879694769"
}, {
"name": "PEPSI",
"_id": "785485686846684684"
}]
}
Expected OutPut:
Miranda name and _id should come of Orders collection by passing _id.
Above sample son i want to get the name value by joining the collections _id, please kindly go through my post and let me know if you have any suggestion.

MongoDB, remove nested doc in an array

I have the following structure in MongoDB and I try to remove the documents that contains specific tags. I can't seem to be able to get the $pull work.
In this example, I would like to pull the nested doc that has has tags :["BB"]
Any help will be appreciated !
{
"_id": 123,
"socialItems": {
"facebook": [{
"name": "firstFacebook",
"id": 2
}, {
"name": "secondFB",
"id": 43
}],
"instagram": [{
"name": "firstNstagram",
"id": 4
}],
"pc": [{
"name": "firstPC",
"id": 55,
"tags": [
"ab"
]
}, {
"name": "secondPC",
"id": 66,
"tags": [
"BB"
]
}]
}
}
I assume you are trying to drop the nested 'pc' doc, from the array? You also don't mention if you're using a specific driver for this, so I've assumed you're running this in the Mongo shell.
The following will remove documents from the 'pc' property, when containing the 'BB' tag.
db.collectionName.update({'socialItems.pc.tags': 'BB'}, {$pull: {'socialItems.pc': {tags: 'BB'}}})

ElasticSearch index from mongodb?

I need create index from mongodb. Colection name is Product and have such structure:
{
"_id": ObjectId("5239656f60663de206b1053e"),
"brand": "<brandName>",
"category": {
"$ref": "Category",
"$id": ObjectId("50cb515760663d3577000043"),
"$db": "<dbName>"
},
"image": "<imageUrl>",
"integraId": "<someId>",
"isActive": <isActive>,
"name": "<productName>",
"slug": "<slug>"
}
Collection Product have more 30 000 rows, but elasticsearch indexing only ~10 000 rows.
My query to create index:
{
"type": "mongodb",
"mongodb": {
"servers": [
{ "host": "127.0.0.1", "port": 27017 }
],
"options": {
"secondary_read_preference": true
},
"db": "<dbName>",
"collection": "Product"
},
"index": {
"name": "test",
"type": "test_type"
}
}
And just a second question: How can I indexing only some fields (name, category (get row by id from other collection) and brand)?
You may have more luck in the Google Groups about it bro http://groups.google.com/group/elasticsearch/topics or in the IRC http://www.elasticsearch.org/community/
MongoDB has full text search built in experimentally in version 2.4 if you would like to experiment with that: http://docs.mongodb.org/manual/core/index-text/ you may be able to query more effeciently. I realize this isn't the same as the elasticsearch solution you're looking for but this might be another way to solve the problem. Good luck!