社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  MongoDB

包含子字段Mongodb的多文件Mongoose查询文档[副本]

J. Davidson • 4 年前 • 848 次点击  

嗨,我在mongodb中有以下文档模型

架构是

const ProductionsSchema=new Schema({
        name: {type: String, required: true, unique: true},
        isActive: {type: Boolean, default: true},
        locations: [{
            name: {type: String},
            isActive : {type: Boolean, default: false}
        }],
        trackno: {type: String}
})
Productions:[{ 
       _id: 125,
        name: 'John Smith',
        locations: [{ name: 'Boston', isActive: true}]
        isActive: true,
        trackno: 2123
    }, 
        { 
       _id: 126,
        name: 'Moe Adam',
        locations: [{ name: 'Chicago', isActive: true}]
        isActive: true,
        trackno: 5663
    }, 
     { 
       _id: 126,
        name: 'Henry Noel',
        locations: [{ name: 'Houston', isActive: false}]
        isActive: true,
        trackno: 4552
    }, 
      { 
       _id: 128,
        name: 'Tim Johnson',
        locations: [{ name: 'Denver', isActive: true}]
        isActive: false,
        trackno: 6672
    }

]

我想找出两个都是真的

Productions.find({"isActive" : true , "locations.isActive": true}, (err, list)=>{
      if(err){
            callback(err);
            }
            callback(null, list)
        })

我正在尝试编写查询,因此两个isActive都是真的。在上面的示例数据中,只有前两条记录应该在答案中。但我不断地得到所有的记录,即使是有“错误”的记录,我甚至试过$elemMatch的位置。isActive仍然没有工作。 请告诉我如何修复此问题,以便我只得到只包含两个isActive的真实值的结果。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51256
 
848 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Neil Lunn
Reply   •   1 楼
Neil Lunn    5 年前

正如原始注释所解释的,您只需要以下查询条件:

{ isActive: true, "locations.isActive": true }

这是一个基本的和条件,您不需要任何特殊的操作符来验证一个条件是否满足数组中任何地方的单个属性,这就是您所要求的全部。

由于这完全按照预期工作,我只能想给你一个完整的工作清单,作为一个基础,找出你做的不同,从而导致你得到的结果与预期不一样。

const { Schema } = mongoose = require('mongoose');

const uri = 'mongodb://localhost:27017/productions';
const opts = { useNewUrlParser: true };

mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('debug', true);

const productionSchema = new Schema({
  name: String,
  isActive: { type: Boolean, default: true },
  locations: [{
    name: String,
    isActive: { type: Boolean, default: false }
  }],
  trackno: Number
})

const Production = mongoose.model('Production', productionSchema);

const data =
[
    {
        name: 'John Smith',
        locations: [{ name: 'Boston', isActive: true}],
        isActive: true,
        trackno: 2123
    },
        {
        name: 'Moe Adam',
        locations: [{ name: 'Chicago', isActive: true}],
        isActive: true,
        trackno: 5663
    },
     {
        name: 'Henry Noel',
        locations: [{ name: 'Houston', isActive: false}],
        isActive: true,
        trackno: 4552
    },
    {
        name: 'Tim Johnson',
        locations: [{ name: 'Denver', isActive: true}],
        isActive: false,
        trackno: 6672
    }
];

const log = data => console.log(JSON.stringify(data, undefined, 2));

(async function() {

  try {
    const conn = await mongoose.connect(uri, opts);

    // clean data
    await Promise.all(
      Object.entries(conn.models).map(([k, m]) => m.deleteMany())
    );

    // set up
    await Production.insertMany(data);

    // Query
    let query = { isActive: true, "locations.isActive": true };
    let results = await Production.find(query);

    log(results);


  } catch(e) {
    console.error(e)
  } finally {
    mongoose.disconnect()
  }

})()

输出两份预期文件:

Mongoose: productions.deleteMany({}, {})
Mongoose: productions.insertMany([ { isActive: true, _id: 5c7f7e9367daed19d6773e9b, name: 'John Smith', locations: [ { isActive: true, _id: 5c7f7e9367daed19d6773e9c, name: 'Boston' } ], trackno: 2123, __v: 0 }, { isActive: true, _id: 5c7f7e9367daed19d6773e9d, name: 'Moe Adam', locations: [ { isActive: true, _id: 5c7f7e9367daed19d6773e9e, name: 'Chicago' } ], trackno: 5663, __v: 0 }, { isActive: true, _id: 5c7f7e9367daed19d6773e9f, name: 'Henry Noel', locations: [ { isActive: false, _id: 5c7f7e9367daed19d6773ea0, name: 'Houston' } ], trackno: 4552, __v: 0 }, { isActive: false, _id: 5c7f7e9367daed19d6773ea1, name: 'Tim Johnson', locations: [ { isActive: true, _id: 5c7f7e9367daed19d6773ea2, name: 'Denver' } ], trackno: 6672, __v: 0 } ], {})
Mongoose: productions.find({ isActive: true, 'locations.isActive': true }, { projection: {} })
[
  {
    "isActive": true,
    "_id": "5c7f7e9367daed19d6773e9b",
    "name": "John Smith",
    "locations": [
      {
        "isActive": true,
        "_id": "5c7f7e9367daed19d6773e9c",
        "name": "Boston"
      }
    ],
    "trackno": 2123,
    "__v": 0
  },
  {
    "isActive": true,
    "_id": "5c7f7e9367daed19d6773e9d",
    "name": "Moe Adam",
    "locations": [
      {
        "isActive": true,
        "_id": "5c7f7e9367daed19d6773e9e",
        "name": "Chicago"
      }
    ],
    "trackno": 5663,
    "__v": 0
  }
]