有一个 MongoDB 集合,其中填充了如下文档:
{
"_id" : ObjectId("5b7f83b591fae49715443590"),
"content" : "{\n\t\"email\":\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b4e485e49555a565e7b5f54565a525515585456" rel="noreferrer noopener nofollow">[email protected]</a>\",\n\t\"country_code\": \"US\"\n}"
}
如您所见,“content”
有一个 JSON 字符串
我能够使用 JSON.parse()
解析字符串,如下所示:
db.getCollection('my_collection').find({}).map(function(doc){ return {'_id':doc._id, 'content': JSON.parse(doc.content)}});
给我一个对象数组,其中 _id
和 content
作为对象。
有没有办法最好在不从 map
获取数组的情况下对 content
执行查询(过滤、聚合函数)?
最佳答案
就像你说的,就数据库而言content
是一个字符串。因此,您需要保存整个集合,其中 content
将是一个对象,以便您能够过滤、聚合等...
关于MongoDB:如何查询 json 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51997839/