NutzCN Logo
问答 Mongo查询问题 请教
发布于 2255天前 作者 Rekoe 2052 次浏览 复制 上一个帖子 下一个帖子
标签:

如果统计event_params集合下属性key=model_id value的string_value=2的数据 可以可以直接通过mongo语句查到么,还是只能在内存中处理?

{
	"_id" : ObjectId("5b987bc343b8ee27c54ca255"),
	"event_date" : "20180910",
	"event_timestamp" : "1536583614871230",
	"event_name" : "check_out",
	"event_params" : [
		{
			"key" : "user_id",
			"value" : {
				"string_value" : "23031"
			}
		},
		{
			"key" : "network",
			"value" : {
				"string_value" : "4"
			}
		},
		{
			"key" : "device_id",
			"value" : {
				"string_value" : "85b111ae1b75c227"
			}
		},
		{
			"key" : "firebase_screen_class",
			"value" : {
				"string_value" : "OrderConfirmationActivity"
			}
		},
		{
			"key" : "time_id",
			"value" : {
				"string_value" : "1536583614870"
			}
		},
		{
			"key" : "version",
			"value" : {
				"string_value" : "1.7.2"
			}
		},
		{
			"key" : "firebase_event_origin",
			"value" : {
				"string_value" : "app"
			}
		},
		{
			"key" : "reg_time",
			"value" : {
				"string_value" : "1536583618"
			}
		},
		{
			"key" : "firebase_screen_id",
			"value" : {
				"int_value" : "-7736801030496289429"
			}
		},
		{
			"key" : "model_id",
			"value" : {
				"string_value" : "2"
			}
		}
	],
	"event_previous_timestamp" : "1536583611395230",
	"event_bundle_sequence_id" : "14",
	"event_server_timestamp_offset" : "54061658",
	"user_pseudo_id" : "d66a564af43617f7d04ec28a601219e6",
	"user_properties" : [
		{
			"key" : "first_open_time",
			"value" : {
				"int_value" : "1536580800000",
				"set_timestamp_micros" : "1536579826411000"
			}
		}
	],
	"user_first_touch_timestamp" : "1536579826411000",
	"device" : {
		"category" : "mobile",
		"mobile_brand_name" : "Samsung",
		"mobile_model_name" : "SM-G610F",
		"mobile_marketing_name" : "J7 Prime",
		"mobile_os_hardware_model" : "SM-G610F",
		"operating_system" : "ANDROID",
		"operating_system_version" : "6.0.1",
		"language" : "ar-il",
		"is_limited_ad_tracking" : "No",
		"time_zone_offset_seconds" : "10800"
	},
	"geo" : {
		"continent" : "Asia",
		"country" : "Saudi Arabia",
		"region" : "Makkah Province",
		"city" : "Jeddah"
	},
	"app_info" : {
		"id" : "com.onemena.momshop",
		"version" : "1.7.2",
		"firebase_app_id" : "1:958449024958:android:a7b5ddeb0af248ee",
		"install_source" : "com.android.vending"
	},
	"traffic_source" : {
		"name" : "(direct)",
		"medium" : "(none)",
		"source" : "(direct)"
	},
	"stream_id" : "1211874123",
	"platform" : "ANDROID"
}
17 回复

集合可以当普通字段查的, 语法不变

@wendal 给写个demo呗

纯粹mongodb的多层对象查询语法...

{
	"event_params" :
		{
			"key" : "model_id",
			"value" : {
				"string_value" : "2"
			}
		}
}

然后转成ZMoDoc表达就好了

db.contacts.count({ "event_name" : "check_out" , "event_params" : { "event_params" : { "key" : "model_id" , "value" : { "string_value" : "2"}}}});
这样写的 查不到数据.

"event_params" : { "event_params" : {

多写了一层呀

@wendal 兽 python下这个怎么写呢?

额,pymongo?没用过,原理都一样的吧

db.contacts.count({ "event_name" : "check_out" , "event_params.key" : "model_id" , "event_params.value" : { "string_value" : "2"}});

db.contacts.count({ "event_name" : "check_out" , "event_params.key" : "model_id" , "event_params.value" : { "string_value" : "2"}});

也就是这个怎么用Python实现

db.example.find({
    '$or':[
       {'$and':[{'example.a':{'$gt':1}},{'example.b':{'$gt':2}}]},
       {'$and':[{'example.c':{'$gt':3}},{'example.d':{'$gt':4}}]}
    ]
})

https://segmentfault.com/q/1010000000620645

table.find({"event_name" : "reg_suc" ,"geo.country" : {"$ne" : "China"} , "device.language" : {"$ne" : "zh-cn"} , "user_first_touch_timestamp" : {"$gte" : str(start_time) , "$lte" : str(end_time)} ,
    '$or':[
        {'$and': [{'platform': 'ANDROID'}, {'event_params.value': {'string_value': '4'},'event_params.key': 'from'}]}
    ]
})

按照这个过滤条件 理论上只能有

{
			"key" : "from",
			"value" : {
				"string_value" : "4"
			}
		}

{
			"key" : "from",
			"value" : {
				"string_value" : "2"
			}
		}
************
{u'value': {u'string_value': u'6'}, u'key': u'from'}
==============
************
{u'value': {u'string_value': u'2'}, u'key': u'from'}
==============
************
{u'value': {u'string_value': u'6'}, u'key': u'from'}
==============
************
{u'value': {u'string_value': u'4'}, u'key': u'from'}
==============
************
{u'value': {u'string_value': u'2'}, u'key': u'from'}
==============
************
{u'value': {u'string_value': u'2'}, u'key': u'from'}
==============
************
{u'value': {u'string_value': u'6'}, u'key': u'from'}
==============
************
{u'value': {u'string_value': u'4'}, u'key': u'from'}
==============
************
{u'value': {u'string_value': u'2'}, u'key': u'from'}
==============
************
{u'value': {u'string_value': u'4'}, u'key': u'from'}
==============
************
{u'value': {u'string_value': u'6'}, u'key': u'from'}
==============

但结果里还有!=4的数据

你指event_params里面还有其他数据?正常呀

来个实现版本的

a = table.find({"event_name" : "reg_suc" ,"geo.country" : {"$ne" : "China"} , "device.language" : {"$ne" : "zh-cn"} , "user_first_touch_timestamp" : {"$gte" : str(start_time) , "$lte" : str(end_time)} ,
'$or':[
        {'$and': [{'platform': 'ANDROID'}, {'event_params': {'value':{'string_value': '4'},'key': 'from'}}]},
        {'$and': [{'platform': 'IOS'}, {'event_params': {'value':{'int_value': '3'},'key': 'from'}}]}
    ]
})

打印是event_params列表里面的值吧? 那个不会过滤的

不是说符合event_params条件的元素才会返回, 是符合event_params条件的那整个记录会一起返回

兽解释的对 就是需要过滤包含的

添加回复
请先登陆
回到顶部