node.js - MongoDB按字段值查询子文档

标签 node.js mongodb mongoose

所以我有一个供应商模式:

 /**
 * 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;

我的系统中的产品有一个“已验证”字段,它是一个 bool 值。在我的其中一条 route ,我想查询数据库以查找拥有未经验证的产品的所有供应商,以便我可以在页面中呈现这些产品。

我尝试了这个,但不幸的是,无论“verified”是真还是假,它都会返回所有子文档:

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 });
    });
};

感谢任何帮助

编辑:

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

{
    "_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
}

我希望我的查询不返回第一个产品,因为“verified == true”

最佳答案

您需要使用$elemMatch查找文档并 $elemMatch用于数据投影

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
      }
    ]
  }
]

检查一下here

关于node.js - MongoDB按字段值查询子文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50973336/

相关文章:

Node.js - 如何使用 crypto.randomBytes 生成特定范围内的随机数

linux - 使用 Solus Linux 安装 MongoDB。我设置了路径变量,但 Mongod 在启动时关闭

javascript - 向现有 MongoDB 文档添加一个字段(在 Node.js 中使用 Mongoose)

mongodb - 将数组中的第一项投影到新字段(MongoDB 聚合)

node.js - mongoose:如何临时存储光标?

json - Express 正在将我的 'Content-Type' header 从 'applicaiton/json' 重写为 'text/plain'

node.js - Socket.io 0.7.9 连接问题

javascript - 在 Puppeteer 中连接浏览器

php - 如何在管理类中将函数作为参数传递

mongodb - 如何在 Mongo 中获取 "(WHERE) column = column"?