How to query nested objects?

前端 未结 3 2198
野性不改
野性不改 2020-11-22 01:43

I have a problem when querying mongoDB with nested objects notation:

db.messages.find( { headers : { From: \"reservations@marriott.com\" } } ).count()
0
db.m         


        
3条回答
  •  日久生厌
    2020-11-22 02:17

    The two query mechanism work in different ways, as suggested in the docs at the section Subdocuments:

    When the field holds an embedded document (i.e, subdocument), you can either specify the entire subdocument as the value of a field, or “reach into” the subdocument using dot notation, to specify values for individual fields in the subdocument:

    Equality matches within subdocuments select documents if the subdocument matches exactly the specified subdocument, including the field order.


    In the following example, the query matches all documents where the value of the field producer is a subdocument that contains only the field company with the value 'ABC123' and the field address with the value '123 Street', in the exact order:

    db.inventory.find( {
        producer: {
            company: 'ABC123',
            address: '123 Street'
        }
    });
    

提交回复
热议问题