zoukankan      html  css  js  c++  java
  • 在mongoose中使用$match对id失效的解决方法

        Topic.aggregate(
    	//{$match:{_id:"5576b59e192868d01f75486c"}}, //not work
    	//{$match:{title:"test"}},
            //{$match:{_id:new mongoose.schema.Types.ObjectId(id)}}, //not work
    	{$match:{_id:new mongoose.Types.ObjectId(id)}},
    	...
    	function(err,res){
    	   ...
    	});
    

    在使用mongoose的聚合时一定要注意,在id列上使用$match会有个小问题,参考上面的代码,{$match:{_id:"5576b59e192868d01f75486c"}}这句话不起作用,但数据库中明明有对应的文档,改为其他列(比如:title)就没问题,但有时就需要使用id列,这时需要这样写{$match:{_id:new mongoose.Types.ObjectId(id)}},经查阅,mongoose中有两个地方定义了ObjectId,mongoose.schema.Types.ObjectIdmongoose.Types.ObjectId,在这里只有后面那种才起作用。

  • 相关阅读:
    Acdream 1174 Sum 暴力
    Acdream 1114 Number theory 莫比乌斯反演
    Acdream 1007 快速幂,模乘法
    UVa 10023
    UVa 11027
    UVa 11029
    UVa 10820
    UVa 10791
    UVa 11121
    UVa 106
  • 原文地址:https://www.cnblogs.com/neverstop/p/4581388.html
Copyright © 2011-2022 走看看