Mongodb 子集合字段上不区分大小写的排序
我知道在mongodb中有,但不能使用它,因为我们的产品不是以3.4运行的 话虽如此,我唯一的其他选择是将聚合管道与排序结合使用 父表是User,每个用户在角色模型中都有一个角色引用 我的查询是这样的,但我甚至无法使投影正常工作:Mongodb 子集合字段上不区分大小写的排序,mongodb,join,Mongodb,Join,我知道在mongodb中有,但不能使用它,因为我们的产品不是以3.4运行的 话虽如此,我唯一的其他选择是将聚合管道与排序结合使用 父表是User,每个用户在角色模型中都有一个角色引用 我的查询是这样的,但我甚至无法使投影正常工作: > db.User.aggregate([{'$lookup': {"from": "Role", "localField": "role", "foreignField&
> db.User.aggregate([{'$lookup': {"from": "Role", "localField": "role", "foreignField": "_id", "as": "role"}}, {'$project': {'role_lower': {'$toLower': '$role.name'}}}]).pretty()
获取错误:无法从BSON类型数组转换为字符串
我做错了什么还是无法实现?$toLower对字符串有效,但查找返回一个数组,在投影到角色之前,您需要先使用$unwind是的,我们必须解开数组结果 以下是您的疑问:
db.user.aggregate([{'$lookup': {"from": "role", "localField": "role", "foreignField": "_id", "as": "role"}},{'$unwind':{path:"$role"}},{'$project':{"_id":1,"name":1,"role":{"$toLower":"$role.name"}}}])
db.user.aggregate([{'$lookup': {"from": "role", "localField": "role", "foreignField": "_id", "as": "role"}},{'$unwind':{path:"$role"}},{'$project':{"_id":1,"name":1,"role":{"$toLower":"$role.name"}}}])