Luc
Luc

Reputation: 17082

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 ?

Upvotes: 3

Views: 495

Answers (1)

Andrew Orsich
Andrew Orsich

Reputation: 53685

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.

Upvotes: 7

Related Questions