Rumah  >  Soal Jawab  >  teks badan

mongodb - mongo数组查询,如何仅返回当前匹配数组?

db.test.find();
{
    _id: ObjectId('55e99a2190df7a911d6ed1f1'),
    list: [
        {
            "aa": 10,
            "data": "test"
        },
        {
            "aa": 1,
            "data": "test"
        }
    ]
}

db.test.find({_id: ObjectId('55e99a2190df7a911d6ed1f1'), list: {'$elemMatch': {'aa': 1}}})
预期结果:
{_id: ObjectId('55e99a2190df7a911d6ed1f1'),
list: {
        "aa": 1,
        "data": "test"
}
PHP中文网PHP中文网2733 hari yang lalu592

membalas semua(4)saya akan balas

  • PHPz

    PHPz2017-04-27 09:04:24

    > db.customer.find().pretty()
    {
        "_id" : ObjectId("559bb1d1d39f0dcac2658f8e"),
        "cust_id" : "abc123",
        "ord_date" : ISODate("2012-10-03T16:00:00Z"),
        "status" : "A",
        "price" : 25,
        "items" : [
            {
                "sku" : "mmm",
                "qty" : 5,
                "price" : 2.5
            },
            {
                "sku" : "nnn",
                "qty" : 5,
                "price" : 2.5
            }
        ]
    }
    {
        "_id" : ObjectId("559bb1ead39f0dcac2658f8f"),
        "cust_id" : "abc123",
        "ord_date" : ISODate("2012-10-03T16:00:00Z"),
        "status" : "B",
        "price" : 30,
        "items" : [
            {
                "sku" : "mmm",
                "qty" : 6,
                "price" : 3.5
            },
            {
                "sku" : "nnn",
                "qty" : 6,
                "price" : 3.5
            }
        ]
    }
    {
        "_id" : ObjectId("559bb200d39f0dcac2658f90"),
        "cust_id" : "abc123",
        "ord_date" : ISODate("2012-10-03T16:00:00Z"),
        "status" : "C",
        "price" : 35,
        "items" : [
            {
                "sku" : "mmm",
                "qty" : 7,
                "price" : 4.5
            },
            {
                "sku" : "nnn",
                "qty" : 7,
                "price" : 4.5
            }
        ]
    }
    > db.customer.find({"items.qty":6},{"items.$":1}).pretty()  //只返回和查询条件匹配的那一个元素  
    {
        "_id" : ObjectId("559bb1ead39f0dcac2658f8f"),
        "items" : [
            {
                "sku" : "mmm",
                "qty" : 6,
                "price" : 3.5
            }
        ]
    }

    Pertanyaan yang sepadan dengan dokumen terbenam, anda juga boleh bertanya secara langsung, seperti:

    > db.customer.find({"items.qty":6}).pretty()
    {
        "_id" : ObjectId("559bb1ead39f0dcac2658f8f"),
        "cust_id" : "abc123",
        "ord_date" : ISODate("2012-10-03T16:00:00Z"),
        "status" : "B",
        "price" : 30,
        "items" : [
            {
                "sku" : "mmm",
                "qty" : 6,
                "price" : 3.5
            },
            {
                "sku" : "nnn",
                "qty" : 6,
                "price" : 3.5
            }
        ]
    }

    Sepadan dengan pelaksanaan pertanyaan anda di atas:

    > db.customer.find({"_id":ObjectId("559bb1ead39f0dcac2658f8f"),items: {'$elemMatch': {'sku': "mmm"}}},{"items.$":1}).pretty()
    {
        "_id" : ObjectId("559bb1ead39f0dcac2658f8f"),
        "items" : [
            {
                "sku" : "mmm",
                "qty" : 6,
                "price" : 3.5
            }
        ]
    }

    db.test.find({_id: ObjectId('55e99a2190df7a911d6ed1f1'), senarai: {'$elemMatch': {'aa': 1}}},{"list.$":1}).cantik( )

    balas
    0
  • PHPz

    PHPz2017-04-27 09:04:24

    Pertama sekali, jika anda menggunakan mongodb, anda mesti faham bahawa mongodb ialah nosql
    Kemudian, koleksi mongo menyimpan dokumen
    Akhir sekali, anda mesti tahu bahawa nilai yang dikembalikan oleh find mestilah koleksi dokumen, yang merupakan tatasusunan

    Jadi apa yang anda katakan pada dasarnya mustahil untuk dicapai

    balas
    0
  • PHP中文网

    PHP中文网2017-04-27 09:04:24

    Hanya isikan syarat pertanyaan yang anda perlukan untuk mencari. Untuk butiran lanjut, sila semak laman web rasmi mongodb

    balas
    0
  • 为情所困

    为情所困2017-04-27 09:04:24

    Gunakan operator $ untuk mengembalikan satu atau lebih, tidak mungkin

    balas
    0
  • Batalbalas