簡體   English   中英

如何查詢MongoDB中的集合值和引用文檔值?

[英]How to query on collection values ​and referenced documents values ​in MongoDB?

我不知道我提出這個問題有多正確。 我需要對集合的值和引用對象的值執行查詢。

原始集合如下所示:

{
  "houses": [
    {
      "_id": "5fe72f0b4fd2c131bcc7dae0",
      "name": "John",
      "district": "Texas",
      "events": [
        {
          "_id": "5fe73e91ede45b3d2eca504c",
          "kind": "developer",
          "group": "facebook"
        }
      ]
    },
    {
      "_id": "5fe72f0b4fd2c131bcc7dadf",
      "name": "Michael",
      "district": "Texas",
      "events": [
        {
          "_id": "5fe73e91ede45b3d2eca504b",
          "kind": "advertiser",
          "group": "instagram"
        }
      ]
    },
    {
      "_id": "5fe72f0b4fd2c131bcc7dade",
      "name": "Frank",
      "district": "Washington",
      "events": [
        {
          "_id": "5fe73e91ede45b3d2eca504a",
          "kind": "developer",
          "group": "school"
        }
      ]
    }
  ]
}

在執行滿足條件的查詢時, district == "Texas" ,我需要得到以下結果:

{
  "houses": [
    {
      "_id": "5fe72f0b4fd2c131bcc7dae0",
      "name": "John",
      "district": "Texas",
      "events": [
        {
          "_id": "5fe73e91ede45b3d2eca504c",
          "kind": "developer",
          "group": "facebook"
        }
      ]
    },
    {
      "_id": "5fe72f0b4fd2c131bcc7dadf",
      "name": "Michael",
      "district": "Texas",
      "events": [
        {
          "_id": "5fe73e91ede45b3d2eca504b",
          "kind": "advertiser",
          "group": "instagram"
        }
      ]
    }
  ]
}

在這種情況下: kind == "developer" ,得到如下結果:

{
  "houses": [
    {
      "_id": "5fe72f0b4fd2c131bcc7dae0",
      "name": "John",
      "district": "Texas",
      "events": [
        {
          "_id": "5fe73e91ede45b3d2eca504c",
          "kind": "developer",
          "group": "facebook"
        }
      ]
    },
    {
      "_id": "5fe72f0b4fd2c131bcc7dade",
      "name": "Frank",
      "district": "Washington",
      "events": [
        {
          "_id": "5fe73e91ede45b3d2eca504a",
          "kind": "developer",
          "group": "school"
        }
      ]
    }
  ]
}

對於滿足條件的查詢: district == "Texas" && kind == "developer" ,得到結果:

{
  "houses": [
    {
      "_id": "5fe72f0b4fd2c131bcc7dae0",
      "name": "John",
      "district": "Texas",
      "events": [
        {
          "_id": "5fe73e91ede45b3d2eca504c",
          "kind": "developer",
          "group": "facebook"
        }
      ]
    }
  ]
}

查詢應該在express路由中使用mongoose執行,並且應該是通用的,處理一組不同的請求參數:

router.get('/report', (req, res) => {
  let params = {}; 
  let { district, kind } = req.headers;

  if (district) params["district"] = district;
  if (kind) params["kind"] = kind;
  // Here should be the query
});

House model 參考Event

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const HouseSchema = new Schema({
  name: String,
  district: String,
  events: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Event'
  }]
});

module.exports = mongoose.model('House', HouseSchema);

我正在學習MongoDBaggregation ,但我對它的所有功能都不太了解。 請告訴我如何以傳統方式正確執行這樣的請求? 我將不勝感激!

db.collection.aggregate([
{$unwind:"$houses"},
{$match:{"houses.district":"Texas","houses.events":{$elemMatch:{"kind":"developer"}}}},
{$group:{
    _id:null,
    houses:{$push:"$houses"}
}},
{$project:{_id:0}}])

暫無
暫無

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

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