我有一个mongodb有很多领域的集合.一个字段是'date_time',它是ISO日期时间格式,Ex:,ISODate("2014-06-11T19:16:46Z")另一个字段是'name'.
鉴于a name,如何找到该系列中最老/最年轻的帖子?
例如:如果two集合'数据'中有帖子:
[{'name' : 'John', 'date_time' : ISODate("2014-06-11T19:16:46Z")},
{'name' : 'John', 'date_time' : ISODate("2015-06-11T19:16:46Z")}]
Run Code Online (Sandbox Code Playgroud)
鉴于"约翰"这个名字,我如何找到该系列中最老的帖子,即ISODate("2014-06-11T19:16:46Z")哪一个?同样对于最年轻的帖子.
如何从具有以下结构的 Mongo 文档中的数组中获取一个元素:
{
array : [
{type: 'cat', name: 'George'}
{type: 'cat', name: 'Mary'}
{type: 'dog', name: 'Steve'}
{type: 'dog', name: 'Anna'}
]
}
Run Code Online (Sandbox Code Playgroud)
例如,我需要得到 Steve,在这种情况下,结果必须如下所示:
{
array : [
{type: 'dog', name: 'Steve'}
]
}
Run Code Online (Sandbox Code Playgroud)
或者: {type: 'dog', name: 'Steve'}
我知道如何在发布时制作它,但我需要在整个数组可用的客户端制作它,我可以使用 forEach 从数组中返回这个值,但我正在寻找更优雅的方式(使用 Mongo 查询)。
我有一个简单的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="form.xsl"?>
<x>
<y>Hello</y>
</x>
Run Code Online (Sandbox Code Playgroud)
和form.xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output encoding="UTF-8" method="xml" indent="yes"/>
<xsl:variable name="topLevel">
<xsl:variable name="inner" select="'Hi'" />
<xsl:value-of select="$inner"/>
</xsl:variable>
<xsl:template match="/" >
<xsl:value-of select="$topLevel" />
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
我正在使用JRE 1.8,,当我使用 转换 XML 时javax.xml.transform.Transformer,我在变量上收到循环引用错误topLevel。
Circular variable/parameter reference in '[variable(topLevel)]
Run Code Online (Sandbox Code Playgroud)
我发现已经有一个与此相关的问题记录在:JIRA,但不确定是否已修复。
提供了各种解决方法,但没有提供解决方案。
注意:我没有包含任何第三方 jar,并且使用默认的 Transformer 实现。
我想做的事情如下:
db.ratings.find().forEach(function(doc){
var item = db.items.find({_id: doc.item_id})
var ry = item.detail.ry
db.ratings.update(doc,{$set: {itd: ry}})
})
Run Code Online (Sandbox Code Playgroud)
问题是db.items.find({_id: doc.item_id})返回了一些我无法直接调用文档属性的东西.这样做的正确方法是什么?谢谢!
db.items:
{
"_id" : ObjectId("5461c8f0426f727f16000000"),
"n" : "The Shawshank Redemption",
"detail" : {
"ry": 1992
}
}
Run Code Online (Sandbox Code Playgroud) 我的 MongoDB 集合由 2 个主要集合组成:
1) 地图
{
"_id" : ObjectId("542489232436657966204394"),
"fileName" : "importFile1.json",
"territories" : [
{
"$ref" : "territories",
"$id" : ObjectId("5424892224366579662042e9")
},
{
"$ref" : "territories",
"$id" : ObjectId("5424892224366579662042ea")
}
]
},
{
"_id" : ObjectId("542489262436657966204398"),
"fileName" : "importFile2.json",
"territories" : [
{
"$ref" : "territories",
"$id" : ObjectId("542489232436657966204395")
}
],
"uploadDate" : ISODate("2012-08-22T09:06:40.000Z")
}
Run Code Online (Sandbox Code Playgroud)
2)在“地图”对象中引用的领土:
{
"_id" : ObjectId("5424892224366579662042e9"),
"name" : "Afghanistan",
"area" : 653958
},
{
"_id" : ObjectId("5424892224366579662042ea"),
"name" : "Angola",
"area" : 1252651 …Run Code Online (Sandbox Code Playgroud) 在将数据变成棘手的格式时遇到一些麻烦.
文件包含以下字段: subject, importance [high,medium,low]
样本文件:
{_id: "", subject: "red", importance: "high"}
Run Code Online (Sandbox Code Playgroud)
我想返回看起来像这样的数据:
[{_id: subject, high: 5, medium: 6, low: 3}]
Run Code Online (Sandbox Code Playgroud)
这些数字与每个重要性级别的文档数量相对应
这是我到目前为止:
{
$group: {
_id: {subject: "$subject", importance: "$importance"},
count: {$sum: 1},
}
},
{
$group: {
_id: "$_id.subject",
data: {
$push: {
importance: "$_id.importance", count: "$count"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以使用值$_id.importance并将其作为键并具有$count值?
var arrayOfNumbers=[1,2,3,4,5,6,78];
for(var index in arrayOfNumbers){
console.log(index+1);
}Run Code Online (Sandbox Code Playgroud)
此示例代码的输出是.
01
11
21
31
41
51
61
为什么在Javascript中这些数组索引被视为字符串?
我mongoDb使用弹性搜索创建了一个映射来索引我的集合。这是mapping属性:
"properties" : {
"address_components" : {
"properties" : {
"_id" : {
"type" : "string"
},
"subLocality1" : {
"type" : "string",
"index" : "not_analyzed"
},
"subLocality2" : {
"type" : "string",
"index" : "not_analyzed"
},
"subLocality3" : {
"type" : "string",
"index" : "not_analyzed"
},
"city" : {
"type" : "string",
"index" : "not_analyzed"
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我想从这些字段中检索整体独特的项目:subLocality1, subLocality2, subLocality3, city。此外,每个distinct值都应包含q为一个子字符串。不同的项目还应包含相应的city值。
例子:
"address_components" : …Run Code Online (Sandbox Code Playgroud) 我在实体中有这样的字段:
private String userId;
private String username;
private Date created;
private List<Comment> comments = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
Comment 有这样的领域:
private String userId;
private String username;
private String message;
private Date created;
Run Code Online (Sandbox Code Playgroud)
我需要进行聚合,并收到类似这样的内容:
{
"userId" : "%some_userId%",
"date" : "%some_date%",
"commentsQty" : 100,
"isCommented" : true
}
Run Code Online (Sandbox Code Playgroud)
我的聚合看起来像这样:
{ "aggregate" : "%entityName%" , "pipeline" : [
{ "$project" : {
"username" : 1 ,
"userId" : 1 ,
"created" : 1 ,
"commentQty" : { "$size" : [ "$comments"]}}}]}
Run Code Online (Sandbox Code Playgroud)
它工作正常.但我还需要检查,IF注释数组包含一些注释,具体的userId.我尝试了这个,但它无法执行:
{ "aggregate" …Run Code Online (Sandbox Code Playgroud) 我尝试开始,mongod.exe但我有,我收到以下错误:
C:\MongoDB\Server\30\bin>mongod.exe
2015-12-16T19:12:17.108+0100 I CONTROL 2015-12-16T19:12:17.110+0100 W CONTROL 32-bit servers don't have journaling enabled by default.
Please use --journal if you want durability.
2015-12-16T19:12:17.110+0100 I CONTROL
2015-12-16T19:12:17.120+0100 I CONTROL Hotfix KB2731284 or later update is not installed, will zero-out data files
2015-12-16T19:12:17.132+0100 I STORAGE [initandlisten] **************
2015-12-16T19:12:17.132+0100 I STORAGE [initandlisten] Error: journal files are present in journal directory, yet starting without journaling enabled.
2015-12-16T19:12:17.133+0100 I STORAGE [initandlisten] It is recommended that you start with journaling enabled so that …Run Code Online (Sandbox Code Playgroud)