我正在使用 mongo-go-driver ( https://godoc.org/github.com/mongodb/mongo-go-driver/mongo ),我正在尝试做相当于
db.getCollection('mycollection').aggregate([
{ $lookup: {
from: "anothercollection",
localField: "_id",
foreignField: "foreignID",
as: "matched_docs"
}},
{ $match: { "matched_docs": { $eq: [] } } },
{ $project: { "matched_docs": 0 } },
{ $match: {"dateTimeGMT":{$lt: (new Date(Date.now()-1000*60*60*24)).toISOString()}} }
])
Run Code Online (Sandbox Code Playgroud)
我不知道如何使用这种方法来放置 Javascript 命令。
pipeline := bson.NewArray(
bson.VC.DocumentFromElements(
bson.EC.SubDocumentFromElements(
//yada, yada, yada...
cursor, err := collection.Aggregate(ctx, pipeline)
Run Code Online (Sandbox Code Playgroud)
(总的来说,无论如何,我不喜欢这种方法。我希望能够在 Robo 3T 中设计查询并将它们复制到我的代码中,就像我在 MySQL Workbench 和 PHP 中所做的那样)
此方法在管道中产生一个空的 *bson.Array
pipelineJSON := `[
{ $lookup: {
from: "anothercollection",
localField: "_id",
foreignField: …Run Code Online (Sandbox Code Playgroud) 我有一个带有日期字段的集合:
{
"_id" : ObjectId("5b92b359ddceef5b24502834"),
"dateTimeGMT" : ISODate("2018-08-22T09:29:25.000Z"),
yada, yada, yada
}
Run Code Online (Sandbox Code Playgroud)
我试图使用 mongo-go-driver 的 ParseExtJSONArray 函数在 $match 聚合阶段按日期查找。(我知道如何直接使用 *bson.Array 执行此操作。我问是为了知道使用 ParserExtJSONArray 执行此操作的正确方法,或者如果我遇到了限制。)
我已简化此示例并确认它与上述文档不匹配。
pipeline, err := bson.ParseExtJSONArray(`[
{ "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }
]`)
cursor, err := receivedFromResponseQueue.Aggregate(ctx, pipeline)
Run Code Online (Sandbox Code Playgroud)
以下内容在 mongo shell 中不起作用。(这并不奇怪,因为它会自动转换为 ISODate() 格式)
db.getCollection('received_from_response_queue').aggregate([
{ "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }
])
Run Code Online (Sandbox Code Playgroud)
但这在 mongo shell 中确实有效。
db.getCollection('received_from_response_queue').aggregate([
{ "$match": { "dateTimeGMT": ISODate("2018-08-22T09:29:25.000Z") } }
])
Run Code Online (Sandbox Code Playgroud)
但这会在“pipeline”中返回一个空数组。(因为 ParseExtJSONArray 不处理 JavaScript)
pipeline, err := bson.ParseExtJSONArray(`[
{ "$match": { "dateTimeGMT":ISODate("2018-08-22T09:29:25.000Z") } …Run Code Online (Sandbox Code Playgroud)