MongoDB按字段值查询子文档 [英] MongoDB Query SubDocuments by field valeu

查看:917
本文介绍了MongoDB按字段值查询子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此我为供应商提供了以下架构:

So I have this schema for a Supplier:

 /**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    Address = require('./Address.js'),
    AddressSchema = mongoose.model('Address').schema,
    Product = require('./Product.js'),
    ProductSchema = mongoose.model('Product').schema;

// Create a new schema for the reviews collection with all the relevant information for a review
var Schema = mongoose.Schema;

var Supplier = new Schema(
    {
        name: String,
        address: AddressSchema,
        location: {
            type: {type:String, default: 'Point'},
            coordinates: [Number] // [<longitude>, <latitude>]
        },
        products: [ProductSchema]
    }
);

Supplier.index({location: '2dsphere'});

var SupplierModel = mongoose.model('Supplier', Supplier );
// export the review model
module.exports = SupplierModel;

我系统中的产品有一个已验证"字段,它是一个布尔值.在我的其中一条路线中,我想查询数据库以查找所有未验证产品的供应商,以便随后在页面中呈现这些产品.

Products in my system have a "verified" field which is a boolean. In one of my routes I would like to query the DB to find all the suppliers which have products which aren't verified such that I can then render those products in the page.

我尝试过此方法,但是无论"verified"是true还是false,它都会返回所有子文档:

I tried this, but unofrtunatelly it returns all the subdocuments no matter if "verified" is true or false:

exports.admin_accept_product_get = function (req, res) {
    Supplier.find({'products.verified' : false}, function(err, docs) {
        res.render('admin_accept_product', { user : req.user, suppliers: docs });
    });
};

感谢您的帮助

上一个查询将返回以下数据:

The previous query would return the following data:

{
    "_id" : ObjectId("5b2b839a2cf8820e304d7413"),
    "location" : {
        "type" : "Point",
        "coordinates" : [ 
            -16.5122377, 
            28.4028329
        ]
    },
    "name" : "Tienda1",
    "products" : [ 
        {
            "verified" : true,
            "_id" : ObjectId("5b2b83d32cf8820e304d7420"),
            "title" : "Vodka",
            "inStock" : 15,
            "typeOfItem" : "alcohol",
            "sellingPrice" : 15,
            "image" : "public/upload/15295784515201529168557789bottle.png",
            "typeOfAlcohol" : "vodka"
        }, 
        {
            "verified" : false,
            "_id" : ObjectId("5b2b848f8c59960c44df09cd"),
            "title" : "Whisky",
            "inStock" : 40,
            "typeOfItem" : "alcohol",
            "sellingPrice" : 15,
            "image" : "public/upload/15295786395491529323314298whisky.png",
            "typeOfAlcohol" : "whisky"
        }
    ],
    "__v" : 2
}

我希望查询不返回首个产品,因为已验证== true"

I would like my query to not return the firt product because "verified == true"

推荐答案

您需要使用 $elemMatch 用于数据投影

You need to use $elemMatch to find the document and $elemMatch for projection of the data

db.collection.find({
  products: {
    $elemMatch: {
      verified: false
    }
  }
},
{
  products: {
    $elemMatch: {
      verified: false
    }
  },
  location: 1
})

输出

[
  {
    "_id": ObjectId("5b2b839a2cf8820e304d7413"),
    "products": [
      {
        "_id": ObjectId("5b2b848f8c59960c44df09cd"),
        "image": "public/upload/15295786395491529323314298whisky.png",
        "inStock": 40,
        "sellingPrice": 15,
        "title": "Whisky",
        "typeOfAlcohol": "whisky",
        "typeOfItem": "alcohol",
        "verified": false
      }
    ]
  }
]

此处

这篇关于MongoDB按字段值查询子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆