模型没有返回预期的结果

Posted

技术标签:

【中文标题】模型没有返回预期的结果【英文标题】:Model not returning what is expected 【发布时间】:2015-09-16 13:25:00 【问题描述】:

我有 2 个猫鼬模型/模式(产品和商家)。当对 Products 执行 .find() 时,它会按预期返回所有产品,但在为 Merchants 执行此操作时,它会返回所有内容(包括商家和产品数据),而它应该只是根据定义的模式返回商家。我的 mongoDB 服务器上有一个集合,其中包含我所有的商家和产品。

知道我错过了什么吗?

谢谢

商家模式

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

var MerchantShema = new Schema(
  merchants: [
    merchant_id: Number,
    price_current: Number,
    price_rrp: Number,
    aff_link: String,
    merchant_product_id: Number,
    aw_image_url: String,
    cost_scoop: String,
    created_at: Date,
    updated_at: Date
  ]
);

module.exports = mongoose.model('Merchants', MerchantShema, 'products');

产品型号

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

var ProductSchema = new Schema(
  ean: Number,
  aw_product_id: Number,
  product_name: String,
  product_brand: String,
  product_description: String,
  img_sml: String,
  img_lrg: String,
  cat: String,
  sub_cat: String,
  weight: String,
  rating: String,
  created_at: Date,
  updated_at: Date,
  merchants: [
    merchant_id: Number,
    price_current: Number,
    price_rrp: Number,
    aff_link: String,
    merchant_product_id: Number,
    aw_image_url: String,
    cost_scoop: String,
    created_at: Date,
    updated_at: Date
  ],
  nutrition: [
    calories: Number,
    protein: Number,
    fat: Number,
    sat_fat: Number,
    carbs: Number,
    sugar: Number,
    salt: Number,
    calories: Number
  ],
  ingredients: String,
  flavours: String,
  is_active: Boolean
);

module.exports = mongoose.model('Products', ProductSchema, 'products');

我的路线

  app.get('/api/products', function(req, res) 
    Products.find(function(err, products)
        if (err) return console.error(err);
        return res.send(products);
    )
    .where('is_active').equals('true')
  );

  app.get('/api/merchants', function(req, res)
    Merchants.find(function(err, merchants)
      if (err) return console.error(err);
      return res.send(merchants);
    );
  );

我的收藏示例

[
    
        "_id": "55840f86e4b0ba19c15ee26d",
        "merchant_id": "1111",
        "merchant_aw_id": "1",
        "merchant_name": "test merchant",
        "merchant_url": "google.com",
        "merchant_image": "",
        "created_at": "",
        "updated_at": "",
        "merchants": []
    ,
    
        "_id": "558426f9e4b0ba19c15ee495",
        "ean": "123456789",
        "aw_product_id": "55555",
        "product_name": "Test Product",
        "product_brand": "Test Brand",
        "product_description": "This is a description for the test product",
        "img_sml": "http://images.productserve.com/preview/6/3196/73/20/704322073.jpg",
        "img_lrg": "http://images.productserve.com/preview/6/3196/73/20/704322073.jpg",
        "cat": "Protein",
        "sub_cat": "Protein",
        "weight": "2.5kg",
        "rating": "5",
        "created_at": "",
        "updated_at": "",
        "nutrition": [
            
                "salt": "1",
                "sugar": "1",
                "carbs": "1",
                "sat_fat": "1",
                "fat": "1",
                "protein": "1",
                "calories": "1"
            
        ],
        "ingredients": "",
        "flavours": "",
        "is_active": "true",
        "merchants": [
            
                "merchant_id": 1111,
                "price_current": 9.99,
                "price_rrp": 15.99,
                "aff_link": "google.com",
                "merchant_product_id": 999,
                "aw_image_url": "",
                "cost_scoop": "43p",
                "created_at": "",
                "updated_at": ""
            
        ]
    ,
    
        "ean": "123456789",
        "aw_product_id": "55555",
        "product_name": "Test Product",
        "product_brand": "Test Brand",
        "product_description": "This is a description for the test product",
        "img_sml": "http://images.productserve.com/preview/6/3196/73/20/704322073.jpg",
        "img_lrg": "http://images.productserve.com/preview/6/3196/73/20/704322073.jpg",
        "cat": "Protein",
        "sub_cat": "Protein",
        "weight": "2.5kg",
        "created_at": "",
        "updated_at": "",
        "nutrition": [
            
                "salt": "1",
                "sugar": "1",
                "carbs": "1",
                "sat_fat": "1",
                "fat": "1",
                "protein": "1",
                "calories": "1"
            
        ],
        "ingredients": "",
        "flavours": "",
        "is_active": "true",
        "merchants": [
            
                "merchant_id": 1111,
                "price_current": 9.99,
                "price_rrp": 15.99,
                "aff_link": "google.com",
                "merchant_product_id": 999,
                "aw_image_url": "",
                "cost_scoop": "43p",
                "rating": "5",
                "created_at": "",
                "updated_at": ""
            
        ]
    
]

【问题讨论】:

【参考方案1】:

查看您的商家模型定义。您正在调用导出中的产品:

module.exports = mongoose.model('Merchants', MerchantShema, 'products');

这应该是这样的

module.exports = mongoose.model('Merchants', MerchantShema, 'merchants');

我注意到的另一件事是,您有两个称为“卡路里”的键。

我运行了代码,现在它可以正确地带回商家。但是,当您调用产品时,它会将商家作为该模式的一部分带回,因为商家包含在产品中。另一种方法是使用嵌套模式。您可以在其他线程中阅读这些内容:Mongoose subdocuments vs nested schema

希望对您有所帮助。

【讨论】:

以上是关于模型没有返回预期的结果的主要内容,如果未能解决你的问题,请参考以下文章

异步等待没有按预期工作,它必须返回我未来的结果

DAX强制ALLEXCEPT函数在没有销售的地方返回空白

PL/SQL 过程未返回预期结果

ASP.NET SQL 查询未返回预期结果

立即执行不返回预期结果以在整个表中搜索字符串

Mongoose populate() 返回没有错误的空数组