2011-08-12 14 views
8

私はmongoose APIを見てきましたが、SOとGoogleグループについて多くの質問がありましたが、埋め込みドキュメントの更新はまだ分かりません。埋め込み文書をマングースに更新するには?

私はこの特定のuserListingsオブジェクトをargsの内容で更新しようとしています。ここで

for (var i = 0; i < req.user.userListings.length; i++) { 
    if (req.user.userListings[i].listingId == req.params.listingId) { 
     User.update({ 
      _id: req.user._id, 
      'userListings._id': req.user.userListings[i]._id 
     }, { 
      'userListings.isRead': args.isRead, 
      'userListings.isFavorite': args.isFavorite, 
      'userListings.isArchived': args.isArchived 
     }, function(err, user) { 
      res.send(user); 
     }); 
    } 
} 

はスキーマです:

var userListingSchema = new mongoose.Schema({ 
    listingId: ObjectId, 
    isRead: { 
     type: Boolean, 
     default: true 
    }, 
    isFavorite: { 
     type: Boolean, 
     default: false 
    }, 
    isArchived: { 
     type: Boolean, 
     default: false 
    } 
}); 

var userSchema = new mongoose.Schema({ 
    userListings: [userListingSchema] 
}); 

これはおそらく最初の問題である、動作しません。また見つける:

User.find({ 
    '_id': req.user._id, 
    'userListings._id': req.user.userListings[i]._id 
}, function(err, user) { 
    console.log(err ? err : user); 
}); 

返します

{ stack: [Getter/Setter], 
    arguments: [ 'path', undefined ], 
    type: 'non_object_property_call', 
    message: [Getter/Setter] } 

これはthと等価でなければなりません

db.users.find({'userListings._id': ObjectId("4e44850101fde3a3f3000002"), _id: ObjectId("4e4483912bb87f8ef2000212")}) 

実行中:Mongoのクライアント呼び出しです

mongoose v1.8.1 
mongoose-auth v0.0.11 
node v0.4.10 

答えて

6

は、あなただけのこのような何かを行うことができます。

var listing = req.user.userListings.id(req.params.listingId); 

listing.isRead = args.isRead; 
listing.isFavorite = args.isFavorite; 
listing.isArchived = args.isArchived; 

req.user.save(function (err) { 
    // ... 
}); 

ここに見られるように:http://mongoosejs.com/docs/subdocs.html

Finding a sub-document

Each document has an _id. DocumentArrays have a special id method for looking up a document by its _id.

var doc = parent.children.id(id); 

* *警告* *

として@jachが指摘したように、サブ文書のスキーマを実際の文書のスキーマの前に宣言してを使用できるようにする必要があります方法。

+2

私はTypeError例外」を取得していますThat's

'" – FrederickCook

+0

これはマングース認証の問題ですか?そこにreq.userが割り当てられています。 – FrederickCook

+0

mongoose docuは次のように言っています: "DocumentArraysには埋め込まれたドキュメントを_idプロパティでフィルタリングする特別なメソッドIDがあります(埋め込まれた各ドキュメントには1つずつ取得されます)"。しかし、私はこれを自分で試していない。 – pkyeck

0

が、これは変数名のただのミスマッチですか?

あなたはforループ内user.userListings[i].listingIdが、finduser.userListings[i]._idを持っています。

listingIdまたは_idをお探しですか?すでにユーザーを持っているとき

0

親オブジェクトを保存し、ネストされたドキュメントをmarkModifiedする必要があります。オブジェクト[オブジェクトのオブジェクト]、[オブジェクトのオブジェクト]、[オブジェクトのオブジェクト]、[オブジェクトのオブジェクト]が方法はありません「をID:ところで私たちはそれ

exports.update = function(req, res) { 
    if(req.body._id) { delete req.body._id; } 
    Profile.findById(req.params.id, function (err, profile) { 
    if (err) { return handleError(res, err); } 
    if(!profile) { return res.send(404); } 
    var updated = _.merge(profile, req.body); 
    updated.markModified('NestedObj'); 
    updated.save(function (err) { 
     if (err) { return handleError(res, err); } 
     return res.json(200, profile); 
    }); 
    }); 
}; 
関連する問題