Kanav Narula
Kanav Narula

Reputation: 337

Extracting and printing substring(based on delimiter or pattern) from key value pair in 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.

Upvotes: 1

Views: 331

Answers (1)

alernerdev
alernerdev

Reputation: 2064

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.

Upvotes: 0

Related Questions