0

In mongoose & node.js

I'd like to GET user's friend's (status is OK) device information. in One query.

UserSchema is below.

var UserSchema = new Schema({
    id : { //emaild
        type : String,
        unique : true
    },
    device : []
    ade : {

        friend : [FriendSchema]
    } 
});

FriendSchema.

var FriendSchema = new Schema({
    user_id : {
        type : Schema.Types.ObjectId,
        ref  : 'User'
    },
    status : {
        type : String,
        'enum' : ['ok', 'rec', 'rec_none', 'app', 'deny'] 
    },
});

I tried like this. But, there're so many problems...

  1. Get main user ID from client.
  2. Query main user info.
  3. for loop, get friend's user_id who status is 'ok' and store array.
  4. Query again friend info and store.
  5. Return to client.

I googled and some pages let me do mapreduce but I don't know how can I do it.

Added----------------------

User DB

{
    "_id" : ObjectId("55f2258cef24cf80230e0b36"),
    "id" : "a@gmail.com",
    "device" : [
        {
            "os" : "iOS",
            "version" : "8.4.1",
            "token" : "(null)",
            "locale" : "en"
        }
    ],
    "ade" : {
        "friend" : [
            {               
                "status" : "app",
                "user_id" : ObjectId("55f22820ef24cf80230e0b37")
            }
        ]
    }
}
{
    "_id" : ObjectId("55f66fefef24cf80230e0b3a"),
    "id" : "b@gmail.com",
    "ade" : {
        "friend" : [
            {
                "status" : "ok",    
                "user_id" : ObjectId("55f22820ef24cf80230e0b37")
            }
        ]
    },
    "device" : [
        {
            "os" : "Android",
            "version" : "6.0",
            "token" : "t",
            "locale" : "en"
        },
        {
            "os" : "Android",
            "version" : "6.0",
            "token" : "t",
            "locale" : "en"
        }
    ]
}
{
    "_id" : ObjectId("55f22820ef24cf80230e0b37"),
    "id" : "c@gmail.com",
    "ade" : {
        "friend" : [
            {
                "status" : "ok",
                "user_id" : ObjectId("55f66fefef24cf80230e0b3a")
            },
            {
                "status" : "rec",
                "user_id" : ObjectId("55f2258cef24cf80230e0b36")
            }
        ]
    },
    "device" : [
        {
            "os" : "Android",
            "version" : "4.4.2",
            "token" : "t",
            "locale" : "en"
        }
    ]
}
j2rry
  • 3
  • 2
  • Can you describe your problem with an example, sample documents and the expected output? – chridam Oct 11 '15 at 08:08
  • Current problem is No.5(return result to client) is executed before get result No.4. – j2rry Oct 11 '15 at 08:30
  • I added user db example. :) – j2rry Oct 11 '15 at 08:39
  • Not sure if mapreduce is ideal for this, perhaps you should look at [**populate**](https://github.com/Automattic/mongoose/wiki/3.6-Release-Notes#added-documentpopulatedpath) method. This question may guide you http://stackoverflow.com/questions/19222520/populate-nested-array-in-mongoose – chridam Oct 11 '15 at 09:10
  • 1
    Greate. It works. Thank you @chridam – j2rry Oct 12 '15 at 11:10

0 Answers0