MongoDB:如何查询json字符串? [英] MongoDB: How to query over a json string?
问题描述
有一个这样的文档填充的MongoDB集合:
There's a MongoDB collection that was populated with documents like this one:
{
"_id" : ObjectId("5b7f83b591fae49715443590"),
"content" : "{\n\t\"email\":\"username@domain.com\",\n\t\"country_code\": \"US\"\n}"
}
如您所见"content"
具有JSON字符串
As you can see "content"
has a JSON string
我能够使用JSON.parse()
来解析字符串,例如:
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)}});
给我一个以_id
和content
作为对象的对象数组.
Giving me an array of objects with _id
and content
as an object.
是否有一种方法可以在content
上执行查询(过滤,聚合函数),最好是不从map
获取数组?
Is there a way to execute queries (filtering, aggregate functions) over content
preferably without getting an array from map
?
推荐答案
在数据库方面,content
是一个字符串.因此,您需要保存整个集合,其中content
将成为对象,以便能够进行过滤,聚合等...
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...
这篇关于MongoDB:如何查询json字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!