Thanks in advance for any help guys! So, I have two collections: A and B.
A is a collection of personal information:
{
"_id": "3453hkj54h5k34j5hkjh"
"location": "New York, U.S.",
"first-name": "Archer",
"last-name": "Vice",
"industry": "intelligence"
},
{
"_id": "3453hkj5sdfdddjh",
"location": "London, UK",
"first-name": "Harry",
"last-name": "Potter",
"industry": "security"
},
{
"_id": "345dfdf5sdfdddjh",
"location": "D.C., US",
"first-name": "Obama",
"last-name": "Barack",
"industry": "president"
}
B is a collection of location information in united state:
{
"_id": "998sdfdsfhejf",
"city": "New York",
"zip": "10122",
"state": "NY",
"lat": 40.749,
"longt": -73.9885
},
{
"_id": "998sdfsdfdsfhejf",
"city": "D.C."
"zip": "20500",
"state": "DC",
"lat": 38.8951,
"longt": -77.0369
}
I what to find out who lives in US by comparing the location field in A against city field in B. B should be a sub string of A, as A often carries state, or country information.
I already converted B to an array by:
var f = db.collection.find(), n = [];
for (var i = 0; i < f.length(); i++) n.push(f[i]['field']);
now B is var n=["D.C.", "New York"]
I know how to check if something is in the array. you do:
db.database.find({
field:
{
$in: array
}
});
To check substring you do this:
db.database.find({A: /substring/ });
or
db.database.find({A: {$regex: 'substring'}});
expected results are
{
"_id": "3453hkj54h5k34j5hkjh",
"location": "New York, U.S.",
"first-name": "Archer",
"last-name": "Vice",
"industry": "intelligence"
},
{
"_id": "345dfdf5sdfdddjh",
"location": "D.C., US",
"first-name": "Obama",
"last-name": "Barack",
"industry": "President"
}
"D.C., US" contains substring "D.C." which is a value in the array n=["D.C.", "New York"].
I know I can do it through mapreduce, but it really just seems to be a one liner. I'm also learning how to join these two collections.
targetnow always an array?