2014-07-10 171 views
0

如果数组以dB为单位存在,我想检查if语句。到目前为止,我在光标中检查上述语句,但我猜测它会降低查询速度。我的代码到现在为止是:检查mongodB中是否存在元素

编辑: 线= [line.rstrip()线路开放(INPUT_FILE)

print len(lines) 
row_no = len(lines) 
col_no = len(lines) 
matrix = sparse.lil_matrix((len(lines), len(lines))) 

no_row = 0 
counter = 0 
for item in lines: 
    # find from database those items which their id exists in lines list and contain a follower_list 
    for cursor in collection.find({"_id.uid": int(item)}): 
     if cursor['list_followers'] is None: 
       continue 
     else:    
      id = cursor['_id']['uid'] 
      counter+=1 
      print counter 
      print id 
      name = cursor['screenname'] 
      # text.write('%s \n' %name) 
      followers = cursor['list_followers']  
      print len(followers) 
      for follower in followers: 
       try: 
        if (follower in lines) and (len(followers)>0): 
         matrix[no_row, lines.index(follower)] = 1 
         print no_row, " ", lines.index(follower), " ", matrix[no_row, lines.index(follower)] 
       except ValueError: 
        continue 
      no_row+=1 
      print no_row 

scipy.io.mmwrite(output_file, matrix, field='integer') 

最后我发现,延误是由于创建稀疏的.lil_matrix

+0

不知道我是否在跟踪这个问题。您是否要求以最有效的方式查看文档中是否存在数组字段,并确实有一些内容? –

+0

“,但我猜测*它会降低查询速度” - 这就是问题所在, –

+0

是的,这就是我的问题! –

回答

1

我能想到的最近的事情是实现一个sparse index和查询有点不同。我将构建一个示例来演示:

{ "a" : 1 } 
{ "a" : 1, "b" : [ ] } 
{ "a" : 1 } 
{ "a" : 1, "b" : [ ] } 
{ "b" : [ 1, 2, 3 ] } 

从本质上讲,你似乎什么要问的是刚刚拿到最后的文件作为匹配不扫描的一切。这是一个不同的查询和稀疏索引帮助的地方。首先查询:

db.collection.find({ "b.0": { "$exists": 1 } }) 

只返回1个项目,因为这是与它的一些内容在现有数组的第一个索引位置。现在指数:

db.collection.ensureIndex({ "b": 1 },{ "sparse": true }) 

但由于查询性质,我们必须.hint()这样的:

db.collection.find({ "b.0": { "$exists": 1 } }).hint({ "b": 1 }).explain() 

是可以获得1号文件,并只考虑了3个文件,实际上有一个数组。