mongodb $在数组内的数组中查找

时间:2017-05-27 06:28:58

标签: node.js mongodb mongoose

您好我想从产品系列中获取产品详细信息,产品ID存在于订单集合中的数组中。 我有这样的订单集:

{
"_id": ObjectId("59267748c1f80b17cd53a7bd"),
"updatedAt": ISODate("2017-05-25T06:33:41.827Z"),
"createdAt": ISODate("2017-05-25T06:18:48.264Z"),
"createdBy": "58ba6b8356a7bf05f613cda0",
"status": "shipment",
"productDetails": [
    {
        "_id": ObjectId("59267997c1f80b17cd53a7da"),
        "productCatalogId": "592677eac1f80b17cd53a7be",
        "quantity": 2,
        "products": [
            {
                "productId": "5926791bc1f80b17cd53a7d1",
                "_id": ObjectId("59267a94c1f80b17cd53a7ee")
            },
            {
                "productId": "5926793bc1f80b17cd53a7d3",
                "_id": ObjectId("59267aacc1f80b17cd53a7ef")
            }
        ]
    },
    {
        "_id": ObjectId("592679a1c1f80b17cd53a7db"),
        "productCatalogId": "592678a5c1f80b17cd53a7c5",
        "quantity": 1,
        "products": [
            {
                "productId": "59267959c1f80b17cd53a7d5",
                "_id": ObjectId("59267ac5c1f80b17cd53a7f0")
            }
        ]
    }
],
"__v": 0,
"OrderedFor": "58ba6b8356a7bf05f613cda0",
"billingId": ObjectId("592679d2c1f80b17cd53a7e2"),
"shipmentId": ObjectId("592679d2c1f80b17cd53a7e3")
 }

和产品系列是:

{
"_id": ObjectId("5926791bc1f80b17cd53a7d0"),
"updatedAt": ISODate("2017-05-25T06:32:52.590Z"),
"createdAt": ISODate("2017-05-25T06:26:35.529Z"),
"productCatalogId": "592677eac1f80b17cd53a7be",
"batch": "2017-05-25T06:26:35.510Z",
"createdBy": "58be7c914e5db93569289d27",
"products": [
    {
        "updatedAt": ISODate("2017-05-25T06:26:35.528Z"),
        "createdAt": ISODate("2017-05-25T06:26:35.528Z"),
        "serialno": "1016SAM0001",
        "mfgdate": ISODate("2017-04-21T18:30:00.000Z"),
        "mfgbatch": "1016",
        "updatedBy": "58be7c914e5db93569289d27",
        "_id": ObjectId("5926791bc1f80b17cd53a7d1"),
        "status": "sold",
        "addOnSerialnos": [
            "1016RAK0001",
            "1016SUJ0001"
        ]
    }
],
"__v": 0
}

我的查询

Order.aggregate([
    { $match: { _id: req.params.id } },
    { $unwind: "$productDetails.products" },
    {
        $lookup: {
            from: "Product",
            localField: "productDetails.products",
            foreignField: "products._id",
            as: "products_docs"
        }
    }, {
        $match: { "products_docs": { $ne: [] } }
    }
], function(err, result) {
    console.log(result);
    return res.json(result);
});

我的订单型号

var mongoose = require('mongoose');
 var Schema = mongoose.Schema;

 var orderProductSchema = new Schema({
 productCatalogId: { type: Object, ref: "ProductCatalog" },
 products: [{ productId: { type: Object, ref: "Product.products" } }],
 quantity: { type: Number, default: 1 }
});

var orderSchema = new Schema({
OrderedFor: { type: Object, ref: 'User' },
billingId: { type: Object, ref: "Billing" },
shipmentId: { type: Object, ref: "Shipment" },
productDetails: [orderProductSchema],
status: { type: String, default: "init" },
createdBy: { type: Object, ref: 'User' },
updateBy: { type: Object, ref: 'User' },
 }, {
 timestamps: true
 });

module.exports = mongoose.model('Order', orderSchema);

和我的产品型号

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var productSchema = new Schema({
imeino: String,
serialno: String,
addOnSerialnos: [String],
mfgdate: Date,
mfgbatch: String,
status: { type: String, default: "in-stock" },
updatedBy: { type: Object, ref: "User" },
}, {
timestamps: true
});

var productDetailSchema = new Schema({
productCatalogId: { type: Object, ref: 'ProductCatalog' },
batch: String,
inventoryId: { type: Object, ref: "Inventory" },
createdBy: { type: Object, ref: "User" },
products: [productSchema],
}, {
timestamps: true
});

module.exports = mongoose.model('Product', productDetailSchema);

哪个无效(获取空数组),有谁可以帮我解决这个问题?

0 个答案:

没有答案