Arrays 如何从数组中检索数据[Mongodb]
我试图从集合中的数组中提取数据,代码的提取如下所示:Arrays 如何从数组中检索数据[Mongodb],arrays,json,mongodb,Arrays,Json,Mongodb,我试图从集合中的数组中提取数据,代码的提取如下所示: > db.nodes.findOne() { "_id" : NumberLong(24060429), "_t" : "OsmNode", "uname" : "studerap", "uid" : 7260, "version" : 2, "changeset" : 634057, "timestamp" : ISODat
> db.nodes.findOne()
{
"_id" : NumberLong(24060429),
"_t" : "OsmNode",
"uname" : "studerap",
"uid" : 7260,
"version" : 2,
"changeset" : 634057,
"timestamp" : ISODate("2007-11-27T11:18:58Z"),
"tags" : [
[
"created_by",
"almien_coastlines"
],
[
"source",
"PGS"
]
],
"tagKeys" : [
"created_by",
"source"
],
"location" : [
5.5442938804626465,
-6.488432884216309
]
}
我实际上想要从位置数组中检索的数据是5.54429388046465。是否应通过索引检索
感谢您帮助从所有文档中提取数组的索引元素,您可以通过:
var index = 0, count = 1;
var cursor = db.nodes.find({}, {_id:1, location:{$slice:[index, count]}});
当然,你可以直接写:
var cursor = db.nodes.find({}, {_id:1, location:{$slice:[0, 1]}});
或
然后,提取结果:
var results = [];
cursor.forEach(function(e) {
results.push(e.location[0]);
});
顺便说一下,
.find()
返回一个光标;和.findOne()
返回一个文档,这相当于在游标有文档时运行一次.find().next()
。我尝试了:db.nodes.find({data.location[0]})或db.nodes.find(data.location[0]),但这些语法不正确db.nodes findOne().location[0]
没有数据
是,我能够检索到第一个索引。但是现在,当我为位置索引0处的所有数据尝试db.nodes.find().location[0]时,它不起作用
var results = [];
cursor.forEach(function(e) {
results.push(e.location[0]);
});