-
Notifications
You must be signed in to change notification settings - Fork 1
/
createIndex.js
42 lines (39 loc) · 872 Bytes
/
createIndex.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
const connection = new Mongo(`localhost:27017`),
db = connection.getDB(`Craiglist`),
geoCollection = db.getCollection(`Cars`);
let result = 0;
geoCollection.updateMany(
{},
[
{
$set: {
location: {
// Using aggregation pipeline $cond operator
// to populate `location` with null
// if lat or long is empty.
$cond: {
if: {
$or: [
{
$eq: ["$long", ""]
},
{
$eq: ["$lat", ""]
}
]
},
then: null,
else: {
type: `Point`,
coordinates: ["$long", "$lat"]
}
}
}
}
},
{
$unset: ["long", "lat"]
}
]
);
geoCollection.createIndex( { location: `2dsphere` } );