mongo search not working on keywords - mongodb

{
"_id": ObjectId("4ed8d496c605da94400001e4"),
"status": 1,
"user": {
"uid": 1
},
"nid": 10582,
"form": {
"your-name": "Bob Smith",
"description": "",
"photo": "",
"address": "123 Turk Hill Rd",
"city": "",
"zip": "14450"
},
"location": {
"address": "123 Turk Hill Rd",
"city": "",
"zip": "14450",
"geo_lat": 43.0329181,
"geo_lng": -77.4391148,
"address_confirmed": "123 Turk Hill Rd, Victor, NY 14564, USA",
"address_status": 200,
"accuracy": 8
},
"keywords": {
"0": "bob",
"1": "smith",
"2": "",
"4": "123",
"5": "turk",
"6": "hill",
"7": "rd",
"9": "14450"
},
"time": ISODate("2011-12-02T13: 37: 26.0Z")
Search:
{
nid: 10582,
keywords: {"$in": ['turk']}
}
Results: none!
What am I doing wrong?

Answer is simple: because of keywords is not an array. To search on keywords you need to change document structure as follow:
{
...
"keywords": [
"bob",
"smith",
"123",
"turk",
"hill",
"rd",
"14450"
],
...
}
It usually happens when you from driver serialize dictionary. In current moment there is no way to search in such structure. Simple use arrays instead of dictionaries. Or you can convert dictionary to array before serialize document and viсe versa when deserialize document.

Related

Find Object within an Array that is within an object also within an Array

I've been struggling with the following Mongo document:
{
"name": "Fire Name",
"address": "123 Somestreet Ave",
"city": "Boston",
"state": "MA",
"zip": "02109",
"dispatchReference": "123codefromdispatch",
"created": {
"$date": "2021-02-26T12:30:41Z"
},
"lastPar": {
"$date": "2021-02-26T22:30:41Z"
},
"latitude": "-83.691407",
"longitude": "141.338391",
"par": 3,
"vehicles": [
{
"_id": "60397691c09c2fd299c40420",
"hidden": false,
"vehicleName": "Updated",
"vehicleNumber": 20,
"vehicleStatus": "Enroute",
"latitude": "-83.691407",
"longitude": "141.338391",
"par": 6,
"lastPar": "2021-02-26T22:30:41+00:00",
"members": [
{
"_id": "60397b14f2a2b10978693a47", <---- find Member with this ID
"hidden": false, <----- update this property
"rank": "firefighter",
"dateOfRank": "2021-02-26T22:49:56+00:00",
"role": "hose",
"firstName": "Reyna",
"lastName": "Casey",
"dateOfBirth": "1992-02-26T22:49:56+00:00",
"sex": "male",
"age": 29,
"dateOfExperience": 5,
"departmentLocation": "975 Pine Street, Gasquet, Montana, 1843",
"lastIncidentID": "60397b14095d4f4313fbf716",
"bpm": 120,
"vomax": "33.1",
"temp": "104.69",
"latitude": "-81.393671",
"longitude": "-78.26867",
"scba": {
"maskStatus": false,
"oxygenLevel": "low"
}
I'm attempting to write a Mongo query that when given the member._id, will update that specific member's hidden property from false to true. So I need to tunnel down into my vehicles array of objects, and find the member within the members array with the matching _id.
I suspect it has something to do with the $[] operator but I'm not having any luck.
Try this:
db.testcollection.updateOne(
{
// Specify some condition related to "vehicles" array.
"vehicles.members._id": ObjectId("60397b14f2a2b10978693a47")
},
{
$set: {
"vehicles.$.members.$[obj].hidden": true
}
},
{
arrayFilters: [
{
"obj._id": ObjectId("60397b14f2a2b10978693a47")
}
]
}
);

Filter Nested Array Items CosmosDb

Is it possible to filter array items in CosmosDb? for example I just need customer info and the first pet(in an array)
Current result:
[
{
"CustomerId": "100",
"name": "John",
"lastName": "Doe",
"pets": [
{
"id": "pet01",
"CustomerId": "100",
"name": "1st pet"
},
{
"id": "pet02",
"CustomerId": "100",
"name": "2nd pet"
}
]
}
]
Expected:
[
{
"CustomerId": "100",
"name": "John",
"lastName": "Doe",
"pets": [
{
"id": "pet01",
"CustomerId": "100",
"name": "1st pet"
}
]
}
]
You can use ARRAY_SLICE function.
SQL:
SELECT c.CustomerId,c.name,c.lastName,ARRAY_SLICE(c.pets,0,1) as pets
FROM c
Result:
[
{
"CustomerId": "100",
"name": "John",
"lastName": "Doe",
"pets": [
{
"id": "pet01",
"CustomerId": "100",
"name": "1st pet"
}
]
}
]

How to get this output in spark sql?

How do I get this output of listing all the movies for each year using spark.sql?
Ouput:
(1988,{(Rain Man),(Die Hard)})
(1990,{(The Godfather: Part III),(Die Hard 2),(The Silence of the Lambs),(King of New York)})
(1992,{(Unforgiven),(Bad Lieutenant),(Reservoir Dogs)})
(1994,{(Pulp Fiction)})
this is the json data:
{ "id": "movie:1", "title": "Vertigo", "year": 1958, "genre": "Drama", "summary": "A retired San Francisco detective suffering from acrophobia investigates the strange activities of an old friend's wife, all the while becoming dangerously obsessed with her.", "country": "USA", "director": { "id": "artist:3", "last_name": "Hitchcock", "first_name": "Alfred", "year_of_birth": "1899" }, "actors": [ { "id": "artist:15", "role": "John Ferguson" }, { "id": "artist:16", "role": "Madeleine Elster" } ] }
Here is the code I have tried:
val hiveCtx = new org.apache.spark.sql.hive.HiveContext(sc)
val movies = hiveCtx.jsonFile("movies.json")
movies.createOrReplaceTempView("movies")
val ty = hiveCtx.sql("SELECT year, title FROM movies")
Please help me find the correct query.
Thanks for you help.
You can get something similar without using spark.sql. You can simply perform the operation on the dataframe itself:
movies.groupBy($"year").agg(concat_ws("; ", collect_list($"title"))).show
Dataset used:
{ "id": "movie:1", "title": "Vertigo", "year": 1958, "genre": "Drama", "summary": "A retired San Francisco detective suffering from acrophobia investigates the strange activities of an old friend's wife, all the while becoming dangerously obsessed with her.", "country": "USA", "director": { "id": "artist:3", "last_name": "Hitchcock", "first_name": "Alfred", "year_of_birth": "1899" }, "actors": [ { "id": "artist:15", "role": "John Ferguson" }, { "id": "artist:16", "role": "Madeleine Elster" } ] }
{ "id": "movie:2", "title": "The Blob", "year": 1958, "genre": "Drama", "summary": "The Blob", "country": "USA", "director": { "id": "artist:3", "last_name": "Hitchcock", "first_name": "Alfred", "year_of_birth": "1899" }, "actors": [ { "id": "artist:15", "role": "John Ferguson" }, { "id": "artist:16", "role": "Madeleine Elster" } ] }
Output:
+----+----------------------------------+
|year|concat_ws(; , collect_list(title))|
+----+----------------------------------+
|1958| Vertigo; The Blob|
+----+----------------------------------+

Postgres JSONB datatype - How to extract data from JSON (of type JsonB) field of postgres database?

Hello Friends,
I need a help to solve the following issue,
I have set of record into my postgres db table, where table has JSONB type field.
JSONB type column contains following JSON,
Record#1 :-
{
"key1": "value1",
"key2": "value2",
"audience": [
{
"name": "Person1",
"email": "test1#mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
},
{
"name": "Person2",
"email": "test2#mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
}
]
}
Record#2:-
{
"key1": "value1",
"key2": "value2",
"audience": [
{
"name": "Person3",
"email": "test3#mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
},
{
"name": "Person4",
"email": "test4#mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
}
]
}
Expected Result (Get All Audience) :-
[
{
"name": "Person1",
"email": "test1#mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
},
{
"name": "Person2",
"email": "test2#mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
},
{
"name": "Person3",
"email": "test3#mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
},
{
"name": "Person4",
"email": "test4#mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
}
]
Can Anyone help me to design a query either native query or through spring-data-jpa ?
I appreciate really if anyone who can help me to carry out from this situation!
You should extract 'audience' array elements of each row with jsonb_array_elements() and aggregate them to a single json object with jsonb_agg():
select jsonb_agg(value)
from my_table
cross join jsonb_array_elements(json_data->'audience')
Working example in rextester.

MongoDB sort does not work

since this morning (I think) I can't sort my collection. The collection has 26944 objects inside (it is currently my biggest - but that cant be the reason right?).
So what i'm doing:
myCollection.find().sort({ _id: 1 })
this works great and without any problem!
but:
myCollection.find().sort({ xxx: 1 })
each other case don't work, xxx means any other key on my object it does not matter which I take...
for example:
myCollection.find({modified: { $exists : true } })
Returns an result with 26944 Objects,
myCollection.find({modified: { $exists : true } }).sort({"modified":-1})
will return 0 Objects.
Did I something wrong?
Maybe I had crashed my collection? Did somebody know what could do in this case? (excepts dropping the collection)
Example Object:
{
"_id": ObjectId("55071e25760e250d050ed8d5"),
"sysModified": new Date("2015-03-15T21:10:12+0100"),
"created": new Date(1426529829922),
"modified": new Date(1426528563945),
"payments": [
],
"orderItems": [
{
"orderId": 'xxxxx',
"itemId": 'xxxxx',
"ean": "XXXXX"
"quantity": 1,
"name": "xxxxxxx",
"vat": 19,
"price": 29.989999999999998437,
"currency": "EUR",
"_id": ObjectId("5507365c89866d820dcef7e0"),
},
{
"orderId": 'xxxxx',
"itemId": 'xxxxx',
"ean": "XXXXX"
"quantity": 1,
"name": "xxxxxxx",
"vat": 19,
"price": 29.989999999999998437,
"currency": "EUR",
"_id": ObjectId("5507365c89866d820dcef7e1"),
}
],
"orderData": {
"orderId": XXXXXXXXX,
"type": "order",
"status": "XXXXXXXXX",
"timestamp_php": XXXXXXXXX,
"customer": XXXXXXXXX,
...
},
"orderDeliveryAddress": {
"customer": xxxxxxxxxxx,
"company": "",
"additional": "",
"firstName": "xxxxxxxxxxx",
"surname": "xxxxxxxxxxx",
"street": "xxxxxxxxxxx",
"houseNumber": "xxxxxxxxxxx",
"zip": "xxxxxxxxxxx",
"city": "xxxxxxxxxxx",
"iso2": "xxxxxxxxxxx",
"phone": "xxxxxxxxxxx",
"fax": "xxxxxxxxxx",
"email": "xxxxxxxxxxx",
},
"orderCustomerAddress": {
"customer": xxxxxxxxxxx,
"company": "",
"additional": "",
"firstName": "xxxxxxxxxxx",
"surname": "xxxxxxxxxxx",
"street": "xxxxxxxxxxx",
"houseNumber": "xxxxxxxxxxx",
"zip": "xxxxxxxxxxx",
"city": "xxxxxxxxxxx",
"iso2": "xxxxxxxxxxx",
"phone": "xxxxxxxxxxx",
"fax": "xxxxxxxxxx",
"email": "xxxxxxxxxxx",
},
"__v": 1
}
It is possible that your collection is to big. If you try so sort on a key, that has no index this can result in an error if the resultset is to big. The error should look like this:
error: {
"$err" : "too much data for sort() with no index.
add an index or specify a smaller limit",
"code" : 10128
}
What you can do is add an index to your key you want to sort by:
db.myCollection.ensureIndex({"modified":-1})
Or you reduce the resultset before sorting:
myCollection.find({modified: { $exists : true } }).limit(10).sort({"modified":-1})