MongoDB管道展开并检查空数组

时间:2016-03-17 17:53:47

标签: python mongodb pymongo

我展开一个日期对象数组的字段,但是在某些情况下有空数组,这很好。我喜欢使用管道进行相同的处理,但在某些情况下,我想过滤具有空数组的结果。

pipeline.append({"$match": {"date_object": {'$exists': False }}})

我想使用管道格式,但以下代码不会返回任何记录:

pipeline.append({"$match": {"date_object": []}})

以下也不起作用:

results = mongo.db.xxxx.aggregate(pipeline)

然后:

pipeline.append({ "$cond" : [ { "$eq" : [ "$date_object", [] ] }, [ { '$value' : 0 } ], '$date_object' ] } )

我也在尝试:

.$cmd failed: exception: Unrecognized pipeline stage name: '$cond'

但是有了这个,我得到以下错误:

find({"date_object": []})

但是,如果我使用查找import javax.swing.JOptionPane; import javax.sound.sampled.*; import java.net.URL; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.ByteArrayInputStream; import java.util.Date; class AcceleratePlayback { public static void main(String[] args) throws Exception { //double playBackSpeed = 1.5; Works //double playBackSpeed = 1.3; Doesn't work File file1= new File("Sample2.wav"); File file2= new File("DEF.wav"); AudioInputStream ais = AudioSystem.getAudioInputStream(file1); AudioFormat af = ais.getFormat(); int frameSize = af.getFrameSize(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[2^16]; int read = 1; while( read>-1 ) { read = ais.read(b); if (read>0) { baos.write(b, 0, read); } } byte[] b1 = baos.toByteArray(); byte[] b2 = new byte[(int)(b1.length/playBackSpeed)]; for (int i=0; i<(b2.length/frameSize); i++) { for (int j=0; j<frameSize; j++) { b2[(i*frameSize)+j] = b1[(int)((i*frameSize*playBackSpeed)+j)]; } } ByteArrayInputStream bais = new ByteArrayInputStream(b2); AudioInputStream aisAccelerated = new AudioInputStream(bais, af, b2.length/frameSize); AudioSystem.write(aisAccelerated, AudioFileFormat.Type.WAVE, file2); } 查询,我可以得到这些结果。如何使用管道进行此操作。

1 个答案:

答案 0 :(得分:0)

我已经完成了MongoDB shell,但它可以很容易地用python语言翻译成Python。

这是你的要求吗?

我想你有这样的结构:

db.collection.save({foo:1, date_object:[new Date(), new Date(2016,1,01,1,0,0,0)]})
db.collection.save({foo:2, date_object:[new Date(2016,0,16,1,0,0,0),new Date(2016,0,5,1,0,0,0)]})
db.collection.save({foo:3, date_object:[]})
db.collection.save({foo:4, date_object:[new Date(2016,1,05,1,0,0,0), new Date(2016,1,06,1,0,0,0)]})
db.collection.save({foo:5, date_object:[]})

// Get empty arrays after unwind
db.collection.aggregate([
    {$project:{_id:"$_id", foo:"$foo", 
        date_object:{
            $cond: [ {"$eq": [{ $size:"$date_object" }, 0]}, [null], "$date_object" ]
        }
      }
    },
    {$unwind:"$date_object"},
    {$match:{"date_object":null}}
])

// Get empty arrays before unwind
db.collection.aggregate([
    {$match:{"date_object.1":{$exists:false}}},
    {$project:{_id:"$_id", foo:"$foo", 
        date_object:{
            $cond: [ {"$eq": [{ $size:"$date_object" }, 0]}, [null], "$date_object" ]
        }
      }
    },
    {$unwind:"$date_object"}
])
  

仅空date_object

[ 
    {
        "_id" : ObjectId("56eb0bd618d4d09d4b51087a"),
        "foo" : 3,
        "date_object" : null
    }, 
    {
        "_id" : ObjectId("56eb0bd618d4d09d4b51087c"),
        "foo" : 5,
        "date_object" : null
    }
]

最后,如果您只需要空的date_object,则无需聚合,您可以使用find轻松实现它:

db.collection.find({"date_object.1":{$exists:false}},{date_object:0})
  

输出

{
    "_id" : ObjectId("56eb0bd618d4d09d4b51087a"),
    "foo" : 3
}

{
    "_id" : ObjectId("56eb0bd618d4d09d4b51087c"),
    "foo" : 5
}