1

mongodb コレクションを 2d から 2dsphere に変更したいと考えています。 db.users に次の構造があります

{
  "_id" : "1c6930387123e960eecd65e8ade28488",
  "interests" : [
     {
       "_id" : ObjectId("56a8b2a72f2c2a4c0250e896"),
       "coordinates" : [-3.6833, 40.4]
    }
  ],   
}

私はこのようなものが欲しいです:

{
  "_id" : "1c6930387123e960eecd65e8ade28488",
  "interests" : [
     {
       "_id" : ObjectId("56a8b2a72f2c2a4c0250e896"),
       "loc":{
         "type":"Point",
         "coordinates" : [-3.6833, 40.4]
       }
    }
  ],   
}

私はこれを試しました:

db.users.update( {interests:{$elemMatch:{coordinates :  { $exists: true } }}}, { $set: { 'interests.$.place':{"type":"Point", "coordinates":'interests.$.coordinates'} } }, { upsert: false, multi: false });

明らかに、文字通り「'interests.$.coordinates'」を挿入します。

そしてノードでこれを試しました:

users.find({interests:{$elemMatch:
    {coordinates :  
        { $exists: true } }}} ,
    {"interests.coordinates":1,"interests._id":1,"_id":0 }).forEach( function( doc ){
        users.update( {interests:
            {$elemMatch:
                {_id :  doc.interests[0]['_id'] }}}, 
            { $set: { 'interests.$.loc':{"type":"Point", "coordinates":doc.interests[0]['coordinates']} } }, 
            { upsert: false, multi: false },function(err, numberAffected, rawResponse) {
            var modified=numberAffected.result.nModified;
             console.log(modified)
        });
    });

しかし、座標は混合値で挿入されました。

考え?

ありがとう!!

4

1 に答える 1

0

私はこのコードをテストしませんでしたが、これでこの問題が明らかになると思います。

users.find(
    {interests:{ $elemMatch: {coordinates : { $exists: true } }}}).forEach( function( doc ){

        for(var c = 0; c < doc.interests.length; c++) {

            var orig = doc.interests[c].coordinates;
            doc.interests[c].loc: { type:"Point", coordinates: orig };
            //delete doc.interests[c].coordinates;

        };

        users.update({ _id: doc._id }, doc);
    });

コレクション内のすべてのドキュメントを反復処理してドキュメント全体を変更し、それを 1 行で更新してみてください。

警告: コレクション内に「検索」条件に一致するドキュメントが大量にある場合は、ページネーション (スキップ/制限) を使用して、最初の読み込みでのパフォーマンスとメモリの問題を回避する必要があります。

于 2016-04-26T17:48:41.127 に答える