문제

I have a photo model and every photo has a vehicle associated with it:

var ObjectId = mongoose.Schema.ObjectId;
var photoSchema = mongoose.Schema({
    name: { type: String},
    path: { type: String},
    vehicle: { type: ObjectId, ref: 'Vehicle' }
  });

What query can I perform to return all photos that match a given vehicle _id? I think the query looks the same as a normal find, but I'm not sure how to turn an _id into an ObjectId.

도움이 되었습니까?

해결책

You don't need to turn anything, your ObjectId itself is _id but in string format when you send it through JSON to somewhere. Try following:

Photo.find({vehicle: id}, function(err, result){...});

Above id is just your vehicle's ObjectId obtained from any source e.g. User Interface

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top