MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

Cara mencari melalui Object value department.name

Anda dapat meneruskan string kueri Anda di regex dari populate.

5.js

const mongoose = require('mongoose');

let EmployeeSchema = new mongoose.Schema({
    Name: String,
    email: String,
    department: {ref: 'department', type: mongoose.Schema.Types.ObjectId}
});

let DepartmentSchema  = new mongoose.Schema({
name: String
});


run().catch((err) => console.log(err));

async function run() {
    await mongoose.connect('mongodb://localhost:27017/test', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    });
    await mongoose.connection.dropDatabase();

    const EmployeeModel = mongoose.model('employee', EmployeeSchema);
    const DepartmentModel = mongoose.model('department', DepartmentSchema);
    const departmentObj = new DepartmentModel({name: "software developer"});
    const {_id} = await departmentObj.save();
    const employeeObj = new EmployeeModel({email: '[email protected]', Name: "alexa", department: _id});
    const result = await (await employeeObj.save()).populate({path: 'department', match: {
       name: {$regex: 'soft'}
   }}).execPopulate()
   console.log(result)
}




  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. MongoDB 4.0 JRException:Opsi 'kursor' diperlukan, kecuali untuk agregat dengan argumen penjelasan

  2. Atribut model berisi kumpulan objek

  3. Mongodb membuka dokumen bersarang

  4. Mengimpor json dari file ke mongodb menggunakan mongoimport

  5. Masukkan dokumen MongoDB dengan React.js