2017-10-26 5 views
0

게시물의 ObjectId을 통해 객체 배열 (게시물)에서 post (객체)를 가져와야합니다.그의 ObjectId를 아는 배열 객체 가져 오기

const userSchema = new mongoose.Schema({ 
    email: { type: String, unique: true }, 
    username: { type: String, unique: true }, 
    password: String, 
    passwordResetToken: String, 
    passwordResetExpires: Date, 

    tokens: Array, 

    posts: [{ 
    title: String, 
    body: String, 
    location: { lat: Number, lng: Number }, 
    postedAt: { type: Date, default: Date.now }, 
    active: {type: Boolean, default: true} 
    }], 

... 

시도했지만 작동하지 않습니다.

+0

는'postId' 유형의'string' 또는'objectId'입니다 희망이 도움이? 만약'string'이이 코드를 사용한다면'User.find ({ 'posts._id': mongoose.Types.ObjectId (postID)})''몽구스'를 요구하는 것을 잊지 마십시오 –

답변

-1

$ in을 사용하여 사용자를 쿼리 할 수 ​​있습니다. 예를 들어

User.find({posts: { $in: { postId } }})