我对此还很陌生,所以请耐心等待,但我有 2 个收藏。一个称为照片,另一个称为用户。
在 Node 中,我使用 mongoose 获取数据并将其放入 MongoDB 中。我的架构可以正常工作:
var picSchema = new Schema({
uid: String,
pid: String,
oFile: String
});
Run Code Online (Sandbox Code Playgroud)
我想要做的是对于 uid,我想为上传照片的用户添加 ObjectId。我可以将其作为字符串传递,但是我认为我必须将该字段设置为 ObjectId,但似乎我不能这样做?
除非我遗漏了什么,否则我不妨在其中添加用户名并将其用作参考?
我的mongodb集合中的文档如下所示:
{
"_id" : ObjectId("5562d6831683523f449e7d85")
"geometry" : {
"type" : "Polygon",
"coordinates" :
[[
[-122.4,37.81],
[-132.9, 39.9],
[-122.28, 37.80],
[-124.18, 39.81]
]]
}}
Run Code Online (Sandbox Code Playgroud)
我有一个点(长对),我将其称为x和y。我需要查看文档的坐标是否创建了一个多边形,以便这些坐标位于该多边形内。
因此,我当然需要计算描述多边形等各边的线,然后查看该点的坐标是否在其中。但是,现在,让我们假设我们要查询的文档具有整个数组的最大和最小经度值,x并y在其中。
这是我尝试查询此内容的方法:
db.<collection-name>.find(
{'geometry.coordinates':
{$elemMatch:
{
[{$gt:-122.1}, {$lt:-122.0 }], [{$gt: 37.89 },{$lt: 37.91}]
}
}
}
)
Run Code Online (Sandbox Code Playgroud)
但是,结果是:Unexpected token [。我试图按照这里的例子。
我如何查询这样的数组,其中文档中数组的每个元素都有条件?谢谢。
我正在开发一个从相机收集快照并将该快照的记录保存到快照表中的软件。我们有数千台摄像机和数亿个快照记录。每个快照记录仅包含少量数据-id,camera_id,时间戳和注释(字符串字段,以获取其他信息)。
我们开始注意到某些查询中的性能问题,主要是在给定时间范围内获取大量快照。除了升级Postgres所运行的硬件之外,我们还在寻找提高整个系统性能的替代方法。
在使用MongoDB之前,该团队也遇到了类似的情况,其中一个表用于相机,一个表用于快照。他们切换到为每个摄像机使用单独的快照表,从而产生了数千个快照表。这将性能提高了10倍,而不会引起新的问题。
现在,我们在Postgres上面临着同样的问题。即使上述解决方案有效,但似乎并没有针对数据库进行优化。在Postgres中做同样的事情会是一个疯狂的想法吗?通常使用的表数量是否存在已知限制?在该用例中,我找不到很好的信息。
我想一次从MongoDB中检索数据
我使用限制来限制返回的记录数
router.post('/List', function (req, res) {
var db = req.db;
var collection = db.get('clnName');
collection.find({}, { limit: 5 * req.body.requestCount }, function (e, docs) {
res.json(docs);
});
});
Run Code Online (Sandbox Code Playgroud)
在这里,从客户端我正在递增requestCount变量,以便我获得5的倍数数据.我想要实现的是在第一个请求中获取前5个数据,在第二个请求中获取接下来的5个数据,但是发生了什么,我获得前5个数据,然后获得前10个数据.
我应该做些什么来实现我的需要?
将在mongo游标方法中使用批量大小解决我的问题吗?
我已经有一个查询,如下所示:
{$match:{
"when":{$gt: new Date(ISODate().getTime() - 1000 * 60 * 60 * 24 * 30)}
}},
{$project:{
"year":{$year:"$when"},
"month":{$month:"$when"},
"day": {$dayOfMonth:"$when"}
}},
{$group:{
_id:{year:"$year", month:"$month", day:"$day"},
"count":{$sum:1}
}},
{$sort:{
_id: 1
}}
Run Code Online (Sandbox Code Playgroud)
结果如下所示:
{ "_id" : { "year" : 2015, "month" : 10, "day" : 19 }, "count" : 1 }
{ "_id" : { "year" : 2015, "month" : 10, "day" : 21 }, "count" : 2 }
Run Code Online (Sandbox Code Playgroud)
除了过去 30 天的结果,我怎么能以相同的格式获得结果,即使count是 0?
像这样:
{ "_id" : { …Run Code Online (Sandbox Code Playgroud) 我试图根据一些匹配条件获取数据.首先我试过这个:这里ending_date是全日期格式
Offer.aggregate([
{
$match: {
carer_id : req.params.carer_id,
status : 3
}
},
{
$group : {
_id : { year: { $year : "$ending_date" }, month: { $month : "$ending_date" }},
count : { $sum : 1 }
}
}],
function (err, res)
{ if (err) ; // TODO handle error
console.log(res);
});
Run Code Online (Sandbox Code Playgroud)
这给了我以下输出:
[ { _id: { year: 2015, month: 11 }, count: 2 } ]
Run Code Online (Sandbox Code Playgroud)
现在我想检查一年,所以我正在尝试这个:
Offer.aggregate([
{
$project: {
myyear: {$year: "$ending_date"}
}
},
{
$match: …Run Code Online (Sandbox Code Playgroud) 我的数据看起来像这样:
{
"_id" : "9aa072e4-b706-47e6-9607-1a39e904a05a",
"customerId" : "2164289-4",
"channelStatuses" : {
"FOO" : {
"status" : "done"
},
"BAR" : {
"status" : "error"
}
},
"channel" : "BAR",
}
Run Code Online (Sandbox Code Playgroud)
我的聚合/组如下所示:
{
"_id" : {
"customerId" : "$customerId",
"channel" : "$channel",
"status" : "$channelStatuses[$channel].status"
},
"count" : {
"$sum" : 1
}
}
Run Code Online (Sandbox Code Playgroud)
所以基本上对于示例数据,该组应该给我一个按以下分组分组的组:
{"customerId": "2164289-4", "channel": "BAR", "status": "error"}
Run Code Online (Sandbox Code Playgroud)
但是我不能在聚合/组中使用 []-indexing。我应该怎么做?
我需要在mongoengine中查询包含所有嵌入文档的列表.这是我的架构:
class Variant(EmbeddedDocument):
name = StringField(required=True)
value = StringField(required=True)
class Sku(Document):
variants = ListField(EmbeddedDocumentField(Variant))
Run Code Online (Sandbox Code Playgroud)
我可以用mongo shell做到:
db.sku.find({variants: [{'name': 'xxx', 'value': 'xxx'}]}).pretty()
Run Code Online (Sandbox Code Playgroud)
但是我还没想办法在mongoengine中做到这一点.我需要文档中的列表与我在查询中放入的列表完全相同.有任何想法吗?
我正在使用Java Driver 3.0和MongoDB,以便通过Web服务发送JSON.
当我想将Document对象(org.bson.Document)转换为JSON时,我会使用obj.toJson(),当我想将JSON转换为Document对象时,我会使用Document.parse(json).
但是,当我处理文档列表(在JSON中表示如下:)时[{"field1":1, ...}, {"field1":2, ...}],我无法找到一种干净的方式来进行这些转换.
到目前为止,我已经提出了这些"黑客":
从List到JSON:我将文档列表添加为更大文档中名为"list"的字段的值.我将这个大文档转换为JSON,并从获取的String中删除我不需要的内容.
public String toJson(List<Document> docs){
Document doc = new Document("list", docs);
String json = doc.toJson();
return json.substring(json.indexOf(":")+2, json.length()-1);
}
Run Code Online (Sandbox Code Playgroud)从JSON到List:我通过将此"list"字段添加到JSON,将其转换为Document并仅从Document获取此字段的值来执行相反的操作.
public static List<Document> toListOfDocuments(String json){
Document doc = Document.parse("{ \"list\":"+json+"}");
Object list = doc.get("list");
if(list instanceof List<?>) {
return (List<Document>) doc.get("list");
}
return null ;
}
Run Code Online (Sandbox Code Playgroud)我还尝试使用另一个JSON序列化程序(我使用了Google的序列化程序),但它没有提供与toJson()Document对象中的内置方法相同的结果,特别是对于"_id"字段或时间戳.
这样做有什么干净的方法吗?
所以我想运行一个猫鼬查询来查找存在所有 searcharray 标签的帖子。
标签的数量各不相同。
目前,这会返回存在任何标签的帖子。
Post.find({
'tags.name': {
$in : searcharray
}
}, function(err, post) {
console.log(post);
}
);
Run Code Online (Sandbox Code Playgroud)
我检查了文档,但无法将其拼凑在一起。
谢谢
mongodb ×9
mongoose ×2
node.js ×2
bson ×1
database ×1
geojson ×1
geolocation ×1
java ×1
mapreduce ×1
mongoengine ×1
monk ×1
performance ×1
postgresql ×1
python ×1