Mongoose:如何找到与objective或字符串匹配的文档?

时间:2022-06-01 22:12:34

This is mongoose DataModel in NodeJs

这是NodeJs中的mongoose DataModel。

product: {type: mongoose.Schema.Types.ObjectId, ref: 'products', required: true}

产品:{类型:mongoose.Schema.Types。objective, ref: product, required: true}

But in DB, this field is having multiple type of values in documents, have String and ObjectId

但是在DB中,这个字段在文档中有多种类型的值,有字符串和objective

I'm querying this in mongoose

我在问蒙古话

{
    $or: [
        {
            "product": "55c21eced3f8bf3f54a760cf" 
        }
        ,
        {
            "product": mongoose.Types.ObjectId("55c21eced3f8bf3f54a760cf") 
        }
    ]
}

But this is only fetching the documents which have that field stored as ObjectId.

但这只是获取将该字段存储为ObjectId的文档。

Is there any way that it can fetch all the documents having both type of values either String OR ObjectId?

是否有方法可以获取所有具有这两种类型的值的文档?

Help is much appreciated. Thanks

感谢帮助。谢谢

2 个解决方案

#1


3  

There is a schema in Mongoose, so when you query a document, it will search it by this schema type. If you change the model's product type to "string", it will fetch only documents with string IDs.

Mongoose中有一个模式,所以当你查询一个文档时,它会用这个模式类型来搜索它。如果您将模型的产品类型更改为“string”,它将只获取带有字符串id的文档。

Even if there is a way to fetch either a string OR ObjectId, it's smelly to me to have such inconsistency.

即使有一种方法可以获取字符串或ObjectId,我还是觉得这样的不一致性很臭。

I've encountered the same problem, so the solution was to standardize all documents by running a script to update them.

我遇到了同样的问题,所以解决方案是通过运行一个脚本更新所有文档。

db.products.find().forEach(function(product) {
  db.products.update({ type: product.type},{
      $set:{ type: ObjectId(data.type)}
  });
});

The only problem I see there is if this type field is actually an _id field. _id fields in MongoDB are immutable and they can't be updated. If that is your case, you can simply create a new document with the same (but parsed) id and remove the old one.

我看到的唯一问题是,如果这个类型字段实际上是一个_id字段。MongoDB中的_id字段是不可变的,不能更新。如果是这种情况,您可以简单地创建一个具有相同(但已解析)id的新文档,并删除旧文档。

This is what I did: (in Robomongo)

这就是我所做的:

db.getCollection('products').find().forEach(
    function(doc){
        var newDoc = doc;
        newDoc._id = ObjectId(doc._id);

        db.getCollection('products').insert(newDoc);
    }
)

Then delete all documents, which id is a string:

然后删除所有文件,其中id为字符串:

db.getCollection('products').find().forEach(
    function(doc){
        db.getCollection('products').remove({_id: {$regex: ""}})
    }
)

#2


1  

There is another way to do this. If we Update the type to Mixed then it will fetch all the documents with each type, either String or ObjectId

还有另一种方法。如果我们将类型更新为mix,那么它将获取具有每种类型的所有文档,无论是字符串还是ObjectId

Define this in your Schema

在模式中定义它

mongoose.Schema.Types.Mixed

mongoose.Schema.Types.Mixed

Like

就像

product: {type: mongoose.Schema.Types.Mixed, required: true}

产品:{类型:mongoose.Schema.Types。混合,要求:真}

#1


3  

There is a schema in Mongoose, so when you query a document, it will search it by this schema type. If you change the model's product type to "string", it will fetch only documents with string IDs.

Mongoose中有一个模式,所以当你查询一个文档时,它会用这个模式类型来搜索它。如果您将模型的产品类型更改为“string”,它将只获取带有字符串id的文档。

Even if there is a way to fetch either a string OR ObjectId, it's smelly to me to have such inconsistency.

即使有一种方法可以获取字符串或ObjectId,我还是觉得这样的不一致性很臭。

I've encountered the same problem, so the solution was to standardize all documents by running a script to update them.

我遇到了同样的问题,所以解决方案是通过运行一个脚本更新所有文档。

db.products.find().forEach(function(product) {
  db.products.update({ type: product.type},{
      $set:{ type: ObjectId(data.type)}
  });
});

The only problem I see there is if this type field is actually an _id field. _id fields in MongoDB are immutable and they can't be updated. If that is your case, you can simply create a new document with the same (but parsed) id and remove the old one.

我看到的唯一问题是,如果这个类型字段实际上是一个_id字段。MongoDB中的_id字段是不可变的,不能更新。如果是这种情况,您可以简单地创建一个具有相同(但已解析)id的新文档,并删除旧文档。

This is what I did: (in Robomongo)

这就是我所做的:

db.getCollection('products').find().forEach(
    function(doc){
        var newDoc = doc;
        newDoc._id = ObjectId(doc._id);

        db.getCollection('products').insert(newDoc);
    }
)

Then delete all documents, which id is a string:

然后删除所有文件,其中id为字符串:

db.getCollection('products').find().forEach(
    function(doc){
        db.getCollection('products').remove({_id: {$regex: ""}})
    }
)

#2


1  

There is another way to do this. If we Update the type to Mixed then it will fetch all the documents with each type, either String or ObjectId

还有另一种方法。如果我们将类型更新为mix,那么它将获取具有每种类型的所有文档,无论是字符串还是ObjectId

Define this in your Schema

在模式中定义它

mongoose.Schema.Types.Mixed

mongoose.Schema.Types.Mixed

Like

就像

product: {type: mongoose.Schema.Types.Mixed, required: true}

产品:{类型:mongoose.Schema.Types。混合,要求:真}