从过滤结果匹配中获取值

时间:2016-11-21 23:20:57

标签: mongodb aggregation-framework mongodb-aggregation

如何获取返回的$filter匹配的值?

例如使用此代码...

db.col.aggregate([
    {$project : {
        "Place"    : {$filter: {input: "$fields",as: "field",cond: { $eq: [ "$$field.name", "ABC" ]}}},
    }}
]);

我会得到

[{"name":"ABC" ,"value":"DEF"}]

作为Place的值。我想知道如何得到值(DEF

1 个答案:

答案 0 :(得分:0)

感谢Yogesh的评论,想出来了

   # Client program
   from socket import *
   import time
   import sys
   import struct

   def Main():

    srcip = destip = "127.0.0.1"
    srcport, destport = 5005, 5000
    payload = '[TESTING]\n'

    tcp = make_tcp(srcport, destport, payload)
    packet = tcp + payload

    buffersize = 2048
    addr = (destport, serverport)
    clientsock = socket(AF_INET,SOCK_DGRAM)

    def tcp(srcport,destport, payload, seq=123 , ackseq=0,
                 fin=False, syn=True, rst=False, psh=False, ack=False, urg=False,
                 window = 5840):
            offsetres = (5 << 4) | 0
            flags = (fin | (syn << 1) | (rst << 2) |
                     (psh << 3) | (ack << 4) | (urg << 5))
            return struct.pack('HHLLBBHHH',
                               srcport, destport, seq, ackseq, offset_res,
                               flags, window, 0, 0)

    data = "This is the client"

    try:
            start = time.time()

            clientsock.sendto(data, packet, serveraddr, (destip, 0))
            print "Client sent message ...", data, "... waiting for response."

            response, addr = clientsock.recvfrom(buffersize)
            response_id = struct.unpack('!HI', response[4:6])

            end = time.time()
            elapsed = end - start

            print response_id
            print "Elapsed time = ", elapsed

    except:
            print "Exception occured"


    clientsock.close()

    if __name__== "__main__":
        Main()

..想知道是否有办法单一项目。