Extracting and printing substring(based on delimiter or pattern) from key value pair in mongoDB - mongodb

We have a document in mongoDB with key value pairs in which we have two columns "_id" and "value" column which looks like below:
{
"_id" : ObjectId("53cf9048b6e9e884602db85f"),
"value" : "Security ID:\t\tS-1-0-0\tAccount Name:\t\tKanav Narula\tAccount Domain:\t\tINDIA\t
}
Now we want to execute a query on value field.
We want to extract Account Name and Account Domain from value field defined in the above document.
Expected output:
{
"_id" : ObjectId("53cf9048b6e9e884602db85f"),
"value" : "Security ID:\t\tS-1-0-0\tAccount Name:\t\tKanav Narula\tAccount Domain:\t\tINDIA\t,
"Account Name" : Kanav Narula,
"Account Domain" : INDIA
}
can anyone suggest ways to perform this activity in mongoDB.
Thanks in advance.

In order to make it searchable, the components of the Value string should have been separate fields, otherwise, Value is just a large string. The schema is wrong for what you are trying to do. Did you port it from Redis or some other key/value store? MongoDB is not a key/value store, you need to rethink your use case in a new light so to speak.

Related

mongodb - storing relation with id

I have a very basic (might be sily) question.
Suppose I am having product collection with list of product as below.
db.products.findOne
{
"_id" : ObjectId("60db1b449f9a43695c4bb461"),
"name" : "product 1"
}
while storing category as a relation, how should it be stored? (Please check the category_id)
approach 1
db.products.findOne
{
"_id" : ObjectId("60db1b449f9a43695c4bb461"),
"name" : "product 1",
"category_id" : ObjectId("608f981fcfeaa757ca69b4b5")
}
approach 2
db.products.findOne
{
"_id" : ObjectId("60db1b449f9a43695c4bb461"),
"name" : "product 1",
"category_id" : "608f981fcfeaa757ca69b4b5"
}
It depends.
Embed as ObjectId type if:
You will perform $lookUp operations based on this key, then it's better to embed it as ObjectId type.
I highly recommend embedding it as ObjectId
Embed as string type if:
The driver or application which you are using does not support working with the ObjectId type.
In the company I am working for, we have embedded relations as ObjectId, and even though it's easy to pass the data to REST API (since no conversion is needed), it's a big pain to convert string to ObjectId for $lookUp operations.
Note that in most of the languages, you have to handle errors additionally while converting string to ObjectId (since ObjectId has validation rules), whereas this problem is not present for converting ObjectId to string

How can I update only new data or changed value in mongodb?

Update command updates the key with provided Json. I want to update only the object that is not present in db and changed value. How can I do that?
"data" : [
{
"_id" : "5bb6253d861d057857ec3ff0",
"name" : "C"
},
{
"_id" : "5bb625fc861d057857ec3ff1",
"name" : "B"
},
{
"_id" : "5bb625fe861d057857ec3ff2",
"name" : "A"
}
]
my data is like this. So, if one more array object comes in json of only 2 new object comes then it should insert the two data along with the 3 data.
Update the object that is not present in DB:
Use upsert: Upsert creates a new document when no document matches the query criteria. Alternatively, you can add null checks in your query e.g { user_id:null }. This will allow to update the data where a record for the user is not present in DB.
Update changed value:
This can be implemented maintaining a key to store last_updated_at. If the last_updated_at value does not match to the previously_updatede_at that record can be treated at modified
You can implement Change Streams, introduced in MongoDB 3.6 from which you can receive real time changes on your data. You can receive only the data that was changed, filtering by the "update" operation. Furthermore you can also filter for data that is newly inserted filtering by the "insert" operation. Please see Change Streams.

Query object within object

I'm using passport.js to store my users into my mongodb. A user object looks like this
{
"_id" : ObjectId("54893faf0907a100006341ee"),
"local" : {
"password" : [encrypted password],
"email" : "johnsmith#domain.com"
},
"__v" : 0
}
In a mongodb shell how would I go about listing all the emails? I'm finding it difficult to do this as my data sits two level deep within the object. Cheers!
You can use distinct to get a list of a field's distinct values in the collection, using dot notation to reference the embedded field:
db.users.distinct('local.email')

How to search by integer type column using morphia, mongodb, play framework

HI I have following data items in mongo db
{ "id" : 950, "name" : "Name 1" },
{ "id" : 951, "name" : "name 2" }
I have tried mapping id as both Integer and String.
and I used morphia + play to connect mongodb and used DAO of morphia.
I need to do a search by id like (in sql where id like '95%' ) and get the result as list.
List pList = ds.createQuery(Person.class).field("id").startsWith("95").asList(); // this is not working
Any ideas how get this done ??
Having already answered on the play mailing list and the morphia list, i'll answer here so others will see it, too. startsWith() is a text based operation. It doesn't work against numbers. you'll have to use a greaterThan/lessThan query for range checking.

Mongodb - combine data from two collections

I know this has been covered quite a lot on here, however, i'm very new to MongoDB and am struggling with applying answers i've found to my situation.
In short, I have two collections 'total_by_country_and_isrc' which is the output from a MapReduce function and 'asset_report' which contains an asset_id not present in the 'total_by_country_and_isrc' collection or the original raw data collection this was MapReduced from.
An example of the data in 'total_by_country_and_isrc' is:
{ "_id" : { "custom_id" : 4748532, "isrc" : "GBCEJ0100080",
"country" : "AE" }, "value" : 0 }
And an example of the data in the 'asset_report' is:
{ "_id" : ObjectId("51824ef016f3edbb14ef5eae"), "Asset ID" :
"A836656134476364", "Asset Type" : "Web", "Metadata Origination" :
"Unknown", "Custom ID" : "4748532", "ISRC" : "", }
I'd like to end up with the following ('total_by_country_and_isrc_with_asset_id'):
{ "_id" : { "Asset ID" : "A836656134476364", "custom_id" : 4748532,
"isrc" : "GBCEJ0100080", "country" : "AE" }, "value" : 0 }
I know how I would approach with in a relational database but I really want to try and get this working in Mongo as i'm dealing with some pretty large collections and feel Mongo is the right tool for the job.
Can anyone offer some guidance here?
I think you want to use the "reduce" output action: Output to a Collection with an Action. You'll need to regenerate total_by_country_and_isrc, because it doesn't look like asset_report has the fields it needs to generate the keys you already have in total_by_country_and_isrc – so "joining" the data is impossible.
First, write a map method that is capable of generating the same keys from the original collection (used to generate total_by_country_and_isrc) and also from the asset_report collection. Think of these keys as the "join" fields.
Next, map and reduce your original collection to create total_by_country_and_isrc with the correct keys.
Finally, map asset_report with the same method you used to generate total_by_country_and_isrc, but use a reduce function that can be used to reduce the intersection (by key) of this mapped data from asset_report and the data in total_by_country_and_isrc.