0

私は配列に追加しようとしています(またはRubyが言っているのは配列です)が、mongoから次のようなエラーが発生し続けます

$addToSet 修飾子を非配列に適用できません

走ろうとすると

 
 User.collection.update({'id'=> current.id},{'$addToSet'=>{ 'following' => current.id}})
    User.collection.update({'id'=> user.id},{'$addToSet'=>{ 'following' => user.id}})

またはmongomapperバージョン

User.push_uniq(current.id, :following => user.id)
    User.push_uniq(user.id, :followers => current.id)

出力すると

<%= デバッグ @user.following.kind_of? 配列 %>

true を返します

ただし、実行時は

デシベル.ユーザー.find() 

モンゴに直接、私は得る

{ "_id" : ObjectId("4c4a196f15a79004e0000007"), "email" : "test@test.com", "フォロー
owers": null、"following": null、"password_hash": "98f2188de42bce1554d08fbc81
d5c99a2c234933", "password_salt": "25d80a83cfe3d126cdbe9fec2b731ab9ea57c3b8","
ユーザー名": "テスト" }

私は、フォローとフォロワーが null ではなく [] であることを期待していました。

debug @user.followers を出力すると、rails は --- [] を表示します。

ユーザーを作成するための私のモデルは

  キー:ユーザー名、:タイプ => 文字列
  キー :email, :type => 文字列
  キー :password_hash, :type => 文字列
  キー :password_salt, :type => 文字列
  key :followers, :type => 配列
  key :following, :type => 配列

このエラーにより、user.followers は見つかったものの、更新できないと思われます。私が変わるとき

User.push_uniq(current.id, :testing => user.id)

エラーが出ないので、その部分は正しいと思います。助言がありますか?

4

2 に答える 2

1

key :following, Arrayこれは、代わりに を使用してキーを宣言するときに、be in 0.8 で機能しますkey :following, :type => Array

push_uniqと の両方を試しcollection.updateましたが、どちらでもエラーは発生しませんでした。あなたの例では、そのコマンドがmongoに直接渡されているため、代わりにcollection.update使用する必要があります。_id: valueid: value

于 2010-07-26T18:47:47.267 に答える
0

これは、mongomapper でキーを定義することと少し矛盾していることがわかりました。

配列で :type => 修飾子を使用しないでください。

コレクション全体を削除し、:type を削除して、すべてを再作成したところ、動作するようになりました。

于 2010-07-26T18:18:46.893 に答える