简体   繁体   中英

Is this query possible in nosql (with mongodb)?

I use mongoose in my node.js app, and basically have the following models:

// Define Car model
CarSchema = new Schema({
  brand  : String,
  type: String,    
  maxSpeed : Number
});
mongoose.model('Car', CarSchema);
// Define User model
UserSchema = new Schema({
  lastname        : String,
  firstname : String,
  cars   : [CarSchema]
});
mongoose.model('User', UserSchema);

I'm really new to NoSQL and I really want to give it a try but I first need to study if this really fits my needs.

With the above models, will I be able to create a query listing all the Users who have a particular type of car among their personal cars ?

I don't how to do it in mongoose. But in mongodb it possible. So, in mongodb shell query will looks like this:

db.users.find({"cars.type":"sport"})

Above query return all users thats have car with type 'sport' in their nested collection of cars.

Mongodb dot notation documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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