Mongodb get element from object - mongodb

I have a collection on which if I do a find().pretty gives me the following values
{
"_id": ObjectId("55f7a2a9247a911393333681"),
"education": [
"Schooling",
"Plus 2",
"Graduate",
"Post Graduate"
],
"identification": [
],
"jobtype": [
],
"actor": [
]
}
Can I just get the values under "actor" without getting the entire collection and then iterating through the values?

Its simple. You can use the following query,
db.collection.distinct('actor')

Related

Querying Mongo DB nested Documents

I am using complex nested mongo DB document. I have tried to use aggregate with replaceroot but using replaceroot I am able to go for only one sub-level. In my ocument I am having 3 sub levels. I tried to get all document and loop information in python but I ended up using 5nested for loops.
[
{
"_id": "A-38",
"resourcePaths": [
{
"resourcePathName": "some1",
"method": "POST",
"scenarioDetails": [
{
"scenarioId": "4-ABCD",
"scenarioName": "scenaio1",
"propertiesDatatype": {},
"requestData": [
{
"sql": "Select * from tbl",
"insUpdFlag": "Y",
},
{
"digit": "",
"insUpdFlag": "N",
}
],
"updatedBy": "db_insert",
},
{
"scenarioId": "4-QZWBJ3",
"scenarioName": "Scen2",
"requestData": [
{
"sql": "",
"insUpdFlag": "N",
}
],
"updated": "01/31/2023 09:16:41"
}
]
}
],
}
]
I am trying get the scenario details which is having "insUpdFlag" as Y like this
{
"scenarioId": "4-ABCD",
"scenarioName": "scenaio1",
"propertiesDatatype": {},
"requestData": [
{
"sql": "Select * from tbl",
"insUpdFlag": "Y",
},
{
"digit": "",
"insUpdFlag": "N",
}
],
"updatedBy": "db_insert",
}
I tried but giving me entire document. I need some help here. TYIA
db.collection.find({
"resourcePaths.scenarioDetails.requestData.insUpdFlag": "Y"
},

Complex text index in mongodb

I have such property in my document
"Resources" : {
"Data" : [
[
"ru.Title",
"Привет, Мир"
],
[
"ru.ManualExcerpt",
null
],
[
"ru.AutoExcerpt",
"Привет, Мир "
],
[
"ru.Body",
"<p>Привет, Мир</p>\n"
],
[
"en.Title",
"Hello world"
],
[
"en.ManualExcerpt",
null
],
[
"en.AutoExcerpt",
"Hello world "
],
[
"en.Body",
"<p>Hello world</p>\n"
]
]
},
I need to created text index for each of these items. I have tried to do something like this
db.document.ensureIndex(
{
"Resources.Data[0][1]": "text",
"Resources.Data[1][1]": "text",
"Resources.Data[2][1]": "text",
"Resources.Data[3][1]": "text",
"Resources.Data[4][1]": "text",
"Resources.Data[5][1]": "text",
"Resources.Data[6][1]": "text",
"Resources.Data[7][1]": "text"
},
{ name: "text-index" }
But search is still not working. Is it possible to create index in my situation?

Is there a better way to write this Postgres JSONB query?

Below is a sample JSONB array. I'm trying to figure out how to write a query that doesn't require a cross product like this.
select b.id from brand b,jsonb_array_elements (b.tree) a where a#>>'{Name}' = 'Skiing';
Bonus points for helping me translate this to SQL Alchemy
[
{
"Name": "Snowboarding",
"Order": 1,
"Categories": {
"Jackets": [
22002,
23224
],
"Helmets": [
24920
],
"Freestyle Boards": [
20164
],
"Goggles": [
23169,
23280
],
"Hats": [
22966,
21727
],
"Bindings": [
19265
],
"Gloves": [
20461
],
"Boots": [
26374,
19079,
21765,
22669
],
"Freeride Boards": [
18395,
25505
],
"Pants": [
24143,
20957
]
}
},
{
"Name": "Skiing",
"Order": 2,
"Categories": {
"Jackets": [
22518,
25791,
19972
],
"Pants": [
17516,
23113
],
"Goggles": [
25066,
20996
],
"Helmets": [
24378
],
"Hats": [
20009,
21245
],
"Cross-country Skiing": [
17464
],
"Gloves": [
25822
],
"Boots": [
16616
],
"Poles": [
19280
]
}
},....]
SQL solution first:
SELECT brand.id
FROM brand
WHERE brand.tree #> '[{"Name": "Skiing"}]'::jsonb;
As for sqlalchemy version, you can simply use contains in order to generate SQL statement above:
q = (session.query(Brand.id)
.filter(Brand.tree.contains([{"Name": "Skiing"}]))
)

Doctrine ODM Query Builder Subquery

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.

How to query data from mongoDB which column is an array

{
"name": "Grieg And Sibelius Songs",
"status": 1,
"lastupdate": 1306294550,
"intro": "",
"artist": [
"ar9341cd00668311e0a45217d9fa59cf02",
"ar9341cd00668311e0a45217d9fa59cf0x"
],
"publisher": "(Warner Music)",
"release_date": "2006-05-08"
}
I want to find the the data that artist column "ar9341cd00668311e0a45217d9fa59cf02" in array("ar9341cd00668311e0a45217d9fa59cf02","ar9341cd00668311e0a45217d9fa59cf0d","ar9341cd00668311e0a45217d9fa59cf0r") what should I do?
You can use $exists to check the presence of the item in array,
db.yourcollectionname.find({artist: {$exists:'ar9341cd00668311e0a45217d9fa59cf02'}})