【发布时间】:2015-04-21 14:47:22
【问题描述】:
提前感谢大家的帮助!所以,我有两个集合:A 和 B。
A是个人信息的集合:
{
"_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是美国位置信息的集合:
{
"_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
}
我通过比较 A 中的 location 字段和 B 中的 city 字段来找出谁住在美国。B 应该是 A 的子字符串,因为 A 通常带有州或国家/地区信息。
我已经通过以下方式将 B 转换为数组:
var f = db.collection.find(), n = [];
for (var i = 0; i < f.length(); i++) n.push(f[i]['field']);
现在 B 是var n=["D.C.", "New York"]
我知道如何检查数组中是否有东西。你这样做:
db.database.find({
field:
{
$in: array
}
});
要检查子字符串,请执行以下操作:
db.database.find({A: /substring/ });
或
db.database.find({A: {$regex: 'substring'}});
预期结果是
{
"_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" 包含子字符串"D.C.",它是数组n=["D.C.", "New York"] 中的一个值。
我知道我可以通过 mapreduce 做到这一点,但它实际上似乎只是一个单行。我也在学习如何加入这两个集合。
【问题讨论】:
-
那么
target现在总是一个数组吗? -
不,这只是一个字段,抱歉不清楚。我会稍微修改一下帖子。
-
不是很清楚你有什么和你想要什么。 B 是脚本中的数组还是某处的字段还是集合。另外,你为什么要调用你的集合 A 和 B,然后在测试集合中查询一个名为 A 的字段?
-
你能给我们看一份完整的文件吗?
-
B 是一个集合,但它被我转换为一个数组。我只用它作为一个清单。如果您有兴趣,我会向您展示完整的文档。