如何使用Mongoose获取对象数组中的对象

时间:2015-10-23 22:44:36

标签: node.js mongodb mongoose nosql

回复:猫鼬'找'

我正在使用Mongoose.js,我需要获得所有具有relationId =='2'的朋友obj。

这是我的数据集:

quickSort ( array )
quickSortRec( array, 0, array.size - 1 )
quickSortRec (array, left, right)
pivotIndex = findpivot(array, left, right) //use any method
newPivotIndex = partition ( array, left, right, pivotIndex )
if ( newPivotIndex - left > 1 )
quickSortRec( array, left, newPivotIndex - 1 )
if ( right - newPivotIndex > 1 )
quickSortRec( array, newPivotIndex + 1, right )

这是我的查询:     User = mongoose.model('users',userSchema);

   var someId = '2';

     var users = [
{
            username: 'robert',
            email: 'robert@robert.com',
            friends: [
                {
                    relationships:[
                        { 
                            relationId: '1',
                            description: ''
                        }
                    ]
                }
             ]
        },
        {
            username: 'john',
            email: 'john@john.com',
            friends: [
                {
                    relationships:[
                        { 
                            relationId: '2',
                            description: ''
                        }
                    ]
                }
             ]
        }
     ];

任何关于如何通过relationId ='2'获取朋友obj的想法?

谢谢,

1 个答案:

答案 0 :(得分:0)

您正在寻找的是.select方法。

public static ArrayList<String> getAlreadyProcessedPhotos(Context context, ArrayList<String> photos, SQLiteDatabase db)
{
   ArrayList<String> notAlreadyProcessedPhotos = new ArrayList<>();
   ArrayList<String> preProc = new ArrayList()

   for (String item: photos) {
       preProc.add("'" + item + "'");
   } 
   String inClause = TextUtils.join(",", preProc);

   String searchQuery = "SELECT " + DatabaseHelper.PATH_COLUMN + "FROM " + DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.PATH_COLUMN + "NOT IN (" +inClause + ")";
   Cursor cursor = photosDb.rawQuery(searchQuery);

   while(cursor.moveToNext()) 
   {
        notAlreadyProcessedPhotos.add(cursor.getString(0);
   }

   return notAlreadyProcessedPhotos;
}