rvazquezglez
rvazquezglez

Reputation: 2454

MongoDB: How to query over a json string?

There's a MongoDB collection that was populated with documents like this one:

{
    "_id" : ObjectId("5b7f83b591fae49715443590"),
    "content" : "{\n\t\"email\":\"[email protected]\",\n\t\"country_code\": \"US\"\n}"
}

As you can see "content"has a JSON string

I was able to parse the string using JSON.parse() like:

db.getCollection('my_collection').find({}).map(function(doc){ return {'_id':doc._id, 'content': JSON.parse(doc.content)}});

Giving me an array of objects with _id and content as an object.

Is there a way to execute queries (filtering, aggregate functions) over content preferably without getting an array from map?

Upvotes: 2

Views: 4842

Answers (1)

Ariel Henryson
Ariel Henryson

Reputation: 1346

like you said In terms of the database content is a string. So you need to save the entire collection where content will be a object so you will be able to filtering, aggregate etc...

Upvotes: 2

Related Questions