简体   繁体   English

如何在mongodb中查询数组

[英]How to query an array in mongodb

Was trying to filter an array with another condition to query my MongoDB database 试图过滤具有其他条件的数组以查询我的MongoDB数据库

I have tried using the elemMatch to match exactly with the query, but it not working out. 我尝试使用elemMatch与查询完全匹配,但elemMatch

Here is my code my shipment schema 这是我的代码我的运输模式

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

Here is my node js. 这是我的节点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);
    })
});

The code above bring every record in my mongoddb, but if i tried the below, where i ask it to fillter by package status. 上面的代码将我mongoddb中的所有记录都带进去,但是如果我尝试了下面的代码,我会在其中要求它按软件包状态填充。 i got a code crash error 我收到代码崩溃错误

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

i expect to get something like this 我希望得到这样的东西

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

instead i got this 相反,我得到了这个

[
  {
    "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
  }
]

You should use $elemMatch inside the key packages , ie db.getCollection('shipments').find( {warehouseNo: "123"}, { packages: { $elemMatch: { status: "In warehouse" }}}) . 您应该在密钥packages内使用$elemMatch ,即db.getCollection('shipments').find( {warehouseNo: "123"}, { packages: { $elemMatch: { status: "In warehouse" }}})

For Example: I have a collection as below: 例如:我有一个收藏如下:

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

} }

Using query like this: 使用这样的查询:

db.getCollection('bios').find( {_id: 1.0 }, { awards: { $elemMatch: { year: 1967.0 }}}) db.getCollection('bios')。find({_id:1.0},{奖项:{$ elemMatch:{年:1967.0}}})

Gave me a result: 给了我一个结果:

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

Hope this will help you. 希望这会帮助你。

You defined warehouseNo as reference from other table. 您已将WarehouseNo定义为其他表的参考。 It must be some ID. 它必须是一些ID。 Please make sure you are comparing the same 请确保您正在比较相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM