MongoDB:如何查询json字符串? [英] MongoDB: How to query over a json string?

查看:3151
本文介绍了MongoDB:如何查询json字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个这样的文档填充的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)}});

给我一​​个以_idcontent作为对象的对象数组.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆