【问题标题】:Is there an equivalent of WITH from CYPHER (neo4j) in MQL (MongoDB Query Language)?在 MQL(MongoDB 查询语言)中是否有来自 CYPHER (neo4j) 的 WITH 等价物?
【发布时间】:2022-08-17 10:23:44
【问题描述】:
在 MongoDB 中是否可以在一个查询中传递多个变量并使用多个数据库搜索?
Neo4j 中的示例 - 找到第一个人,然后根据第一次搜索找到结果:
MATCH (n {name: \'Anders\'})--(m)
WITH m
ORDER BY m.name DESC
LIMIT 1
MATCH (m)--(o)
RETURN o.name
是否有可能在 MongoDB 中获得类似的东西?
目前我有一个想法,只做两个单独的查询。如果这是唯一的解决方案,应该如何在查询之间优化传输这些数据?我在 nodejs 上使用 mongodb 驱动程序。
标签:
mongodb
neo4j
mongodb-query
cypher
【解决方案1】:
这是一个示例,您使用类似于 Neo4j 的 MongoDb 找到第一个节点。然后使用该节点查找与第一个节点相关的另一个节点。
//create sample data
use test1
db.employees.insertMany([
{empId: 1, name: 'Anders', related: 'Dave' },
{empId: 2, name: 'Dave', related: 'Mark' }
]);
// this is similar to find Anders and sort the result in descending order (-1) and return the related person Dave
temp = db.employees.find({"name": "Anders"}).sort({ "name": -1 }).limit(1).map(function(el) { return el.related } );
// then search the name Dave based on the previous find
db.employees.find({"name": {$in: temp}})
Result:
{ "_id" : ObjectId("62fc509b4f1f4a75855bccec"), "empId" : 2, "name" : "Dave", "related" : "Mark" }