MongoDB text search across two collections - mongodb

I have an Order collection with address fields and User collection with names. The Order collection contains a string called userId, which is a "foreign key" into the users collection.
I am using an aggregation pipeline to filter, join, sort, and paginate queries. The problem is that I need to provide full text search on the address and name fields.
Because the $text match must be the first stage in a pipeline, I am not sure how to accomplish the goal of finding text matching any address or name field.
User collection
[{
"_id": "5cb8caa069fc1a4351cc3705",
"firstName": "James",
"lastName": "Bond"
},{
"_id": "5c58b8de8596d52c248f34d5",
"firstName": "Jack",
"lastName": "Ryan"
}]
Order Collection
[{
"_id": "5ccc94602e67ca44fe69f160",
"address": {
"streetAddress1": "1112 main st",
"streetAddress2": null,
"unitNumber": "unit 1112",
"city": "Jackson Hole",
"state": "WY",
"postalCode": "83001"
},
"userId": "5cb8caa069fc1a4351cc3705"
}]
A search for "Jack" should match both the name "Jack" and the city "Jackson Hole".

Related

mongodb: how to find (query) child documents based on value of parent document

Let's say I have 3 collections:
db.countries
{"_id": ObjectID("612fa2c8cfd018fafd3d80c8"), "name": "USA"}
{"_id": ObjectID("612fc32aaad1e43da8475344"), "name": "Germany"}
db.regions
{"_id": ObjectID("61dc76f645b6e8f9539a41e8"), "name": "New York", "country_id": ObjectID("612fa2c8cfd018fafd3d80c8")}
{"_id": ObjectID("61dc76f645b6e8f9539a41e6"), "name": "West Region", "country_id": ObjectID("612fc32aaad1e43da8475344")}
db.cities
{"_id": ObjectID("61dc76f645b6e8f9539a41e8"), "name": "New York City", "region_id": ObjectID("61dc76f645b6e8f9539a41e8")}
{"_id": ObjectID("61dc76f645b6e8f9539a41e6"), "name": "Berlin", "region_id": ObjectID("612fc32aaad1e43da8475344")}
As you can see I have here simple example of relations: Country >> Region >> City
1)I need to filter cities based on country name, like:
db.cities.findOne({"region_id.country_id.name": "USA"}) For that I can use only Python Motor or mongosh.
2)Could you please send me articles to mongodb and other docs, where I can grasp as much information about Model One-to-Many Relationships with Document References as possible. Because I lack for flexibility of interaction with such topology.

How to combine two the same collections in one collection in mongo db? (like union all in MySql)

for example I have collection_1 with this data:
"_id": "60d75ed08af96529bf605030"
"Fname": "some name"
"Lname": "some last name"
"Age": "20"
"sex": "Woman"
"Previous_job": "Arsen"
and collection two with this data:
"_id": "60d75ed08af96529bf605030"
"Fname": "some name"
"Lname": "some last name"
"Age": "20"
"sex": "Woman"
"Previous_job": "Arsen"
I know how I can make it in MySQL using UNION ALL but I don't know how to make it in mongo db,
how can I combine these collections in one collection?
can give answer in Russian or Ukraine

How to update a property of a sub-document in an embedded array?

Given the following document in the database, I want to update pincode of address array.
I'm using the $ positional locator in Mongodb. But this does not find the document embedded multiple levels.
"_id": ObjectId("58b91ccf3dc9021191b256ff"),
"phone": 9899565656,
"Email": "sumit#mail.com",
"Organization": "xyz",
"Name": "sumit",
"address": [{
"city": "chennai",
"pincode": 91,
"_id": ObjectId("58b91db48682ab11ede79b28"),
"choice": [{
"_id": ObjectId("58b91fa6901a74124fd70d89")
}]
}]
Using this query to update.
db.presenters.update({"Email":"sumit#mail.com","address.city":"chennai"},{$set:{"address.$.pincode.": 95 }})
You seem to have incorrect field name while updating, an extra dot at the end. Try following
db.presenters.update({"Email":"sumit#mail.com","address.city":"chennai"},
{$set:{"address.$.pincode": 95 }})

Only query field if value has been passed from API in MongoDB

I have created an API in Mule and have a number of queryParameters specified in the RAML file that I would like to use to query my store MongoDB collection.
A snippet of the collection looks like this:
{
"stores": [{
"storeId": 1234,
"storeName": "Shop Around Ltd",
"opens": "09:00:00.000Z",
"closes": "17:00:00.000Z",
"departments": ["clothing", "computers", "toys", "kitchen and home"],
"address": {
"street": "street1",
"city": "New York",
"state": "New York",
"zipCode": "10002"
}
}]
}
My problem is that the query parameters used to query the collection may be different each time so how can I write a dynamic query for MongoDB so it only queries on the fields that have been passed in with values instead of writing a query for each combination of query parameters? E.g. I may want to query based on zip code for one query so all the other fields will be blank and the next time I may want to query on the departments and whether the store will be open at 11am.
The query will be called from Mule.
Thanks

postgres + jsonb + get values of key from multidimentional array

I am trying to get jsonb result based on matching key.
I have DB table "listings" with number and data column.
number | data
1 | {"name": "XYZ company", "city": "toronto", "province": "ON", "people" : [
{ "firstName": "tom", "lastName": "hanks",
"phonenumber": [{"type": "mobile", "Number": "111111"}],
"Email": [{"type": "business", "address": "tom#xyz.com"},{"type": "personal", "address": "tom#mailinator.com"}] },
{ "firstName": "sandra", "lastName": "petes",
"phonenumber": [{"type": "mobile", "Number": "333"}, {"type": "home", "Number": "444"}],
"Email": [{"type": "business", "address": "sandra#xyz.com"}]
}
]}
I need to pull all values for data column with keys -
people->firstname
people->lastName
people->phonenumber->Number
people->Email->address
What I achieved so far is:
SELECT number
,jonb_array_length(jsonb_extract_path(data,'people')) as people_count
,jsonb_extract_path(data,'people','0','firstname') as FirstName
,jsonb_extract_path(data,'people','0','lastname') as LastName
,jsonb_extract_path(data,'people','0','email','Address'") as personEmail
,jsonb_extract_path(data,'people','0','phonenumber','Number') as personPhone
FROM listings
WHERE number='1';
However, this only gives me 0th element of people, I need to find all elements. Is there any way to achieve this in single query.
Thanks for your time!
You need to use the jsonb_array_elements() function to get all of the elements of the array. Since that function returns a set of rows, you need to use it as a row source.
SELECT '1' AS number,
jsonb_array_length(data->'people') AS people_count,
people->>'firstname' AS FirstName,
people->>'lastname' AS LastName,
people->'email'->0->>'Address' AS personEmail,
people->'phonenumber'->0->>'Number' as personPhone
FROM listings, jsonb_array_elements(data->'people') p(people)
WHERE number = '1';
This will result in a row for every person where number = '1'. The email and phone number objects are arrays too and I pick here just the first value. If you want all of them you need to just get the whole JSON arrays and then wrap this in an outer query where you do jsonb_array_elements() again.