如果我有一个Friends像这样的集合:
{'_id': ObjectId('abcdef1'), 'user': 'Jim', 'user2': 'Jon'}
{'_id': ObjectId('abcdef2'), 'user': 'Jim', 'user2': 'Fred'}
{'_id': ObjectId('abcdef3'), 'user': 'Jon', 'user2': 'Jim'}
Run Code Online (Sandbox Code Playgroud)
我想要一个查询,让所有与我成为朋友的人,以及我是否与他们成为朋友的布尔值.我希望有一些发现声明如下:
Find all people I'm friends with and add a field that says whether they're friends with me
Run Code Online (Sandbox Code Playgroud)
我的预期输出将是用户吉姆:
{'friends': {'user': 'Jon', 'friends_with_me': true,
{'user': 'Fred', 'friends_with_me': false,}}
Run Code Online (Sandbox Code Playgroud) 我试图从用户表中获取关注者的数量,该表基本上是使用 java 编程的数组列表。
我正在使用下面的查询来使用命令行界面获取计数。
db.userinfo.aggregate([{ $project : {followersCount : {$size: "$followers"}}}])
Run Code Online (Sandbox Code Playgroud)
但我无法在 java 中创建与我新手相同的查询。下面是我用 java 编写的代码,我收到 com.mongodb.MongoCommandException: Command failed with error 17124: 'The argument to $size must be an array, but was of type: int' 错误。
AggregateIterable<Document> elements = collectionUserInfo.aggregate(
Arrays.asList(new BasicDBObject("$project", new BasicDBObject("followers",
new BasicDBObject("$size", new BasicDBObject("followers",1).put("followers",new BasicDBObject("$ifNull", "[]")))))));
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我解决这个问题吗
我正在尝试使用RepositoryRestResource和实现一个rest apiRestTemplate
除了加载@DBRef 之外,一切都运行良好
考虑这个数据模型:
public class Order
{
@Id
String id;
@DBRef
Customer customer;
... other stuff
}
public class Customer
{
@Id
String id;
String name;
...
}
Run Code Online (Sandbox Code Playgroud)
以及以下存储库(客户的类似存储库)
@RepositoryRestResource(excerptProjection = OrderSummary.class)
public interface OrderRestRepository extends MongoRepositor<Order,String>{}
Run Code Online (Sandbox Code Playgroud)
其余 api 返回以下 JSON:
{
"id" : 4,
**other stuff**,
"_links" : {
"self" : {
"href" : "http://localhost:12345/api/orders/4"
},
"customer" : {
"href" : "http://localhost:12345/api/orders/4/customer"
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果由 resttemplate 正确加载,将创建一个新的 Order 实例,其中 customer = null
是否可以在存储库端急切地解决客户并嵌入 …
我将 Spring 数据与 MongoDB 一起使用,我需要查找实际日期和提前 7 天之间的数据。
我已经使用 @Query 注释创建了存储库,并且不喜欢使用 Criteria 类。
您知道如何在 @Query 之间使用吗?
提前致谢。
spring mongodb spring-data-mongodb spring-boot spring-mongodb
我的收藏中有以下文件。每个文档都包含推文的文本和从推文中挑选出的一组实体(使用 AWS Comprehend):
{
"text" : "some tweet by John Smith in New York about Stack Overflow",
"entities" : [
{
"Type" : "ORGANIZATION",
"Text" : "stack overflow"
},
{
"Type" : "LOCATION",
"Text" : "new york"
},
{
"Type" : "PERSON",
"Text" : "john smith"
}
]
},
{
"text" : "another tweet by John Smith but this one from California and about Google",
"entities" : [
{
"Type" : "ORGANIZATION",
"Text" : "google"
},
{
"Type" : "LOCATION", …Run Code Online (Sandbox Code Playgroud) 在我的 Mongo 数据库中,我有两个集合 \xe2\x80\x94speakers和speeches,并且我可以使用以下代码块“加入”它们(获取演讲者的演讲):
// I\'ve already found the \'speaker\' in the database\ndb.collection(\'speakers\').aggregate([{\n $match: { "uuid": speaker.uuid } },\n { $lookup: {\n from: "speeches",\n localField: "uuid",\n foreignField: "speaker_id",\n as: "speeches" } }\n]).toArray();\nRun Code Online (Sandbox Code Playgroud)\n\n它工作得很好,但我想按一个date或多个title字段对演讲数组进行排序,但我所做的一切似乎都无法实现。我在这里看到的示例都没有完成我需要它们做的事情。我尝试在块{ $sort: { "speech.title": -1 } }之后添加$lookup,但它什么也没做。这可能吗?
在 React.js 应用程序的 Node.js 后端中,我使用
"mongodb" (as db.version returns): "3.4.10",
"mongoose": "5.3.4"
Run Code Online (Sandbox Code Playgroud)
问题是我无法将 $expr 与 mongoDB 版本 < 3.6 一起使用。由于这个问题(不推荐使用的方法等),升级 mongoDB 版本似乎需要付出很大的努力。所以我想知道是否有一种方法可以在不使用 $expr 的情况下完成我想做的事情?
这是代码:
match.$expr = {
$lt: ["$distance", "$range"] // "calculated_distance < tutor_range"
}
Run Code Online (Sandbox Code Playgroud)
你知道怎么做吗?这是完整的方法,如果您想了解更多详细信息。
exports.search = (req, res) => {
let lat1 = req.body.lat;
let lon1 = req.body.lng;
let page = req.body.page || 1;
let perPage = req.body.perPage || 10;
let radius = req.body.radius || 10000;
let levelsIn = req.body.levels && req.body.levels.length !== 0 ? req.body.levels.map(level => …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用新的 MongoDB v3.4 $graphLookup 聚合管道元素。我的数据“节点”可以有 0、1 或多个父节点。每个节点还可以有 0 个、1 个或多个子节点。我想使用 $graphLookup 生成以下输出:
{"parentNodeKey": undefined, "parentLabel": undefined, "nodeKey": 1, "nodeLabel": "Node 1"},
{"parentNodeKey": 1, "parentLabel": "Node 1", "nodeKey": 11, "nodeLabel": "Node 1.1"},
{"parentNodeKey": 1, "parentLabel": "Node 1", "nodeKey": 12, "nodeLabel": "Node 1.2"},
{"parentNodeKey": 1, "parentLabel": "Node 1", "nodeKey": 13, "nodeLabel": "Node 1.3"},
{"parentNodeKey": 1, "parentLabel": "Node 1", "nodeKey": 31, "nodeLabel": "Node 3.1"},
{"parentNodeKey": 11, "parentLabel": "Node 1.1", "nodeKey": 111, "nodeLabel": "Node 1.1.1"},
{"parentNodeKey": 11, "parentLabel": "Node 1.1", "nodeKey": 112, "nodeLabel": "Node …Run Code Online (Sandbox Code Playgroud) 我在 MongoDB 中有一个 processingClickLog 集合。
{
"_id" : ObjectId("58ffb4cefbe21fa7896e2d73"),
"ID" : "81a5d7f48e5df09c9bc006e7cc89d6e6",
"USERID" : "206337611536",
"DATETIME" : "Fri Mar 31 17:29:34 -0400 2017",
"QUERYTEXT" : "Tom",
"DOCID" : "www.demo.com",
"TITLE" : "Harry Potter",
"TAB" : "People-Tab",
"TOTALRESULTS" : "1",
"DOCRANK" : 1
}
{ "id":
....
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试在 java 中执行一个复杂的查询。我的查询是获取处理的ClickLog 集合,其中
下面是我的Java代码。我能够满足前三个条件。但是我被困在按 USERID 分组的第四个条件中。
String jsonResult = "";
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("test1");
MongoCollection<Document> collection …Run Code Online (Sandbox Code Playgroud) 所以我在我的人物收藏中有这些文件:
{
"_id" : ObjectId("595c0630939a8ae59053a9c3"),
"name" : "John Smith",
"age" : 37,
"location" : "San Francisco, CA",
"hobbies" : [
{
"name" : "Cooking",
"type" : "Indoor",
"regular" : true
},
{
"name" : "Baseball",
"type" : "Outdoor",
"regular" : false
}
]
}
{
"_id" : ObjectId("595c06b7939a8ae59053a9c4"),
"name" : "Miranda Thompson",
"age" : 26,
"location" : "Modesto, CA",
"hobbies" : [
{
"name" : "Lego building",
"type" : "Indoor",
"regular" : false
},
{
"name" : "Yoga",
"type" : …Run Code Online (Sandbox Code Playgroud) mongodb ×10
group-by ×2
java ×2
spring-boot ×2
graph ×1
json ×1
mongodb-java ×1
mongoose ×1
node.js ×1
spring ×1