栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在mongo

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在mongo

MongoDB扩展JSON背后的想法是用纯JSON
表示二进制JSON(BSON)类型。

通用语法是将对象表示为单个嵌入式文档。例如,BSON
二进制对象表示为document

{"$binary": "<binary data>"}
$
键字段中的前缀指示类型。BSON
日期对象也是如此。

方法bson.ParseExtJSONArray()期望扩展的JSON类型为文档,而不是MongoDB
点表示法表达式。例如,而不是下面:

{ "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }

该方法期望:

{ "$match": { "dateTimeGMT": {"$date":"2018-08-22T09:29:25.000Z" } } }

您还可以在Unix Epoch中提供日期值,例如:

{ "$match": { "dateTimeGMT": {"$date": { "$numberLong": "1546300800"} } } }

使用mongo-go-driver / bson,示例如下:

raw := `[ { "$match": {"dateTimeGMT": {"$date": {"$numberLong": "1530962624753" } } } } ]`pipeline, err := bson.ParseExtJSonArray(raw)cursor, err := collection.Aggregate(context.Background(), pipeline)

特别说明: 您可以

ParseExtJSonArray()
在迭代结果值并将其传递给聚合之前进行调试。例如:

toConvert := `[   { "$lookup": {        "from": "anotherCollection",        "localField": "foreignKey",        "foreignField": "_id",        "as": "someField"    }},    { "$match": {"dateTimeGMT":{"$lt": {"$date": "2019-01-10T09:29:25.000Z" } } } }]`pipeline, err := bson.ParseExtJSonArray(toConvert)it, err := bson.NewArrayIterator(pipeline)for it.Next() {    fmt.Println(it.Value().Mutabledocument().ToExtJSON(true))}//Outputs : //   {"$lookup":{"from":"anotherCollection","localField":"foreignKey","foreignField":"_id","as":"someField"}}//   {"$match":{"dateTimeGMT":{"$lt":{"$date":{"$numberLong":"1547112565000"}}}}}//


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377040.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号