-
Notifications
You must be signed in to change notification settings - Fork 7
/
mongo-select-tokens.js
51 lines (51 loc) · 1.34 KB
/
mongo-select-tokens.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// get one enabled gcm push token of all active non-bot users
printjson(db.getCollection("users").aggregate([
{
$match: {
type: "user",
active: true
}
},
{
$lookup: {
from: "_raix_push_app_tokens",
let: {
uId: "$_id"
},
pipeline: [ // this is a left outer join
{
$match: {
$expr: {
$and:
[
{ $eq: [ "$userId", "$$uId" ] },
{ $eq: [ "$enabled", true ] }
]
}
}
},
{
$limit: 1 // we need just one enabled token per user for the gateway to work
}
],
as: "push"
}
},
{
$match: {
"push.token.gcm": {
$exists: true // no love for iOS users :(
}
}
},
{
$project: {
_id: 0,
uid: "$_id",
uname: "$username",
token: {
$arrayElemAt: [ "$push.token.gcm", 0 ] // at least one token exists at this stage
}
}
}
]).toArray());