簡體   English   中英

如何在mongodb中查詢數組

[英]How to query an array in mongodb

試圖過濾具有其他條件的數組以查詢我的MongoDB數據庫

我嘗試使用elemMatch與查詢完全匹配,但elemMatch

這是我的代碼我的運輸模式

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

// Create Schema
const ShipmentSchema = new Schema({
  warehouseNo:{
    type: String,
    ref: 'users.unitNo'
  },
  packages:[
    {
        category:{
            type: String
        },
        quantity:{
            type: String
        },
        description:{
            type: String
        },
        trackingno:{
            type: String,
        },
        date:{
            type: Date,
            default: Date.now
        },
        length:{
            type: Number
        },
        width:{
            type: Number
        },
        height:{
            type: Number
        },
        weight:{
            type: Number
        },
        fee:{
            type: Number, 
        },
        status: {
            type: String,
            default: "In warehouse"
        },
  },
],

  shippingMode:{
    type: String,
  },

 date:{
        type: Date,
        default: Date.now
 }
});

module.exports = Shipments = mongoose.model('shipments', ShipmentSchema);

這是我的節點js。

// @route   GET api/user/package
// @desc    Get all package
// @access  Private
router.get('/package', 
  passport.authenticate('jwt', { session: false }),

 (req, res) => {
  const errors = {};

  Shipments.findOne({warehouseNo : req.user.unitNo})
    .then(shipments => {
      if (shipments.packages.length === 0) {
        errors.nopackages = 'There are no packages for you yet';
        return res.status(404).json(errors);
      }
      res.json(shipments.packages);
    })
});

上面的代碼將我mongoddb中的所有記錄都帶進去,但是如果我嘗試了下面的代碼,我會在其中要求它按軟件包狀態填充。 我收到代碼崩潰錯誤

// @route   GET api/user/package
// @desc    Get all package
// @access  Private
router.get('/package', 
  passport.authenticate('jwt', { session: false }),

 (req, res) => {
  const errors = {};

  Shipments.find({warehouseNo : req.user.unitNo, "packages.status": "In warehouse"})
    .then(shipments => {
      if (shipments.packages.length === 0) {
        errors.nopackages = 'There are no packages for you yet';
        return res.status(404).json(errors);
      }
      res.json(shipments.packages);
    })
});

我希望得到這樣的東西

{
    "status": "In warehouse",
    "date": "2019-09-11T10:19:02.834Z",
    "_id": "5d78ca160e47be29e13253b5",
    "category": "liquid",
    "quantity": "10 pieces",
    "description": "garri",
    "trackingno": "MHS085533395",
    "weight": 123,
    "length": 12,
    "height": 12,
    "width": 13
  }

相反,我得到了這個

[
  {
    "status": "Shipped",
    "date": "2019-09-11T10:17:46.485Z",
    "_id": "5d78c9ca0e47be29e13253b4",
    "category": "liquid",
    "quantity": "10 pieces",
    "description": "garri",
    "trackingno": "SDG561920753",
    "weight": 123,
    "height": 12,
    "width": 13
  },
  {
    "status": "In warehouse",
    "date": "2019-09-11T10:19:02.834Z",
    "_id": "5d78ca160e47be29e13253b5",
    "category": "liquid",
    "quantity": "10 pieces",
    "description": "garri",
    "trackingno": "MHS085533395",
    "weight": 123,
    "length": 12,
    "height": 12,
    "width": 13
  }
]

您應該在密鑰packages內使用$elemMatch ,即db.getCollection('shipments').find( {warehouseNo: "123"}, { packages: { $elemMatch: { status: "In warehouse" }}})

例如:我有一個收藏如下:

{
"_id" : 1.0,
"name" : {
    "first" : "John",
    "last" : "Backus"
},
"birth" : ISODate("1924-12-03T05:00:00.000Z"),
"death" : ISODate("2007-03-17T04:00:00.000Z"),
"contribs" : [ 
    "Fortran", 
    "ALGOL", 
    "Backus-Naur Form", 
    "FP"
],
"awards" : [ 
    {
        "award" : "W.W. McDowell Award",
        "year" : 1967.0,
        "by" : "IEEE Computer Society"
    }, 
    {
        "award" : "National Medal of Science",
        "year" : 1975.0,
        "by" : "National Science Foundation"
    }, 
    {
        "award" : "Turing Award",
        "year" : 1977.0,
        "by" : "ACM"
    }, 
    {
        "award" : "Draper Prize",
        "year" : 1993.0,
        "by" : "National Academy of Engineering"
    }
]

}

使用這樣的查詢:

db.getCollection('bios')。find({_id:1.0},{獎項:{$ elemMatch:{年:1967.0}}})

給了我一個結果:

{“ _id”:1.0,“ awards”:[{“ award”:“ WW McDowell Award”,“ year”:1967.0,“ by”:“ IEEE Computer Society”}]}

希望這會幫助你。

您已將WarehouseNo定義為其他表的參考。 它必須是一些ID。 請確保您正在比較相同

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM