基本上试图弄清楚是否有更好的方法来做到这一点,至少可以说有点笨拙。
我正在尝试使用三个变量来查询我的mongoDB数据库,并且我想输出一组数字,这些数字表示给定样本中有多少项包含某些集合成员资格/变量的共现(不是co-的概率)发生但实际的共现)。是否有一种更简单的方法来做到这一点,而不仅仅是逐个查询“A和B和C”,然后是“A和B但不是C”等......?
示例查询:鉴于数据库中有100篇报纸文章,关键字A,B和C出现的所有会员资格是多少?
示例输出: (输出格式为10,20,20,5,5,10,20,10)
修改:下面是我要查询的数据类型的示例,主要是词干关键字:
{
"_id" : ObjectId("5af2f38f8ff26c8160a864be"),
"date" : ISODate("2010-03-02T00:00:00.000Z"),
"publication" : "guardian",
"title" : "'After you Conor, age before beauty' – Ian Paisley in his own words",
"keywords" : [
"queen",
"church",
"associ",
"scotland",
"intent",
"parti",
"alli",
"northern",
"secretari",
"age",
"gather",
"rival",
"unit"
],
"article_id" : ObjectId("5a72d72b257e063072ad9605")
}
...以及我目前查询数据库的方式:
counter = article_list.count( { $and: [{keywords: {$in: ["scotland"]}}, {keywords: {$in: ["church"]}},{keywords: {$in: ["queen"]}}] } )
outputArray.append(counter) # A & B & C
counter = article_list.count( { $and: [{keywords: {$in: ["scotland"]}}, {keywords: {$in: ["church"]}},{keywords: {$nin: ["queen"]}}] } )
outputArray.append(counter) # A & B
counter = article_list.count( { $and: [{keywords: {$in: ["scotland"]}}, {keywords: {$nin: ["church"]}},{keywords: {$in: ["queen"]}}] } )
outputArray.append(counter) # A & C
counter = article_list.count( { $and: [{keywords: {$in: ["scotland"]}}, {keywords: {$nin: ["church"]}},{keywords: {$nin: ["queen"]}}] } )
outputArray.append(counter) # ONLY A
counter = article_list.count( { $and: [{keywords: {$nin: ["scotland"]}}, {keywords: {$in: ["church"]}},{keywords: {$in: ["queen"]}}] } )
outputArray.append(counter) # B & C
counter = article_list.count( { $and: [{keywords: {$nin: ["scotland"]}}, {keywords: {$in: ["church"]}},{keywords: {$nin: ["queen"]}}] } )
outputArray.append(counter) # ONLY B
counter = article_list.count( { $and: [{keywords: {$nin: ["scotland"]}}, {keywords: {$nin: ["church"]}},{keywords: {$in: ["queen"]}}] } )
outputArray.append(counter) # ONLY C
print ', '.join(outputArray)
基本上只是想知道是否有更好的方法来执行此操作,有人可以建议,这不涉及循环查询。