简体   繁体   English

如何在javascript中克隆mongoose查询对象

[英]how to clone the mongoose query object in javascript

I am facing the problem of clone of the mongoose query object .我面临着mongoose query对象的克隆问题。 Javascript the copy the one object into another object by call-by-ref but in my project there is scenario i need to copy one object into another object by call-by-value. Javascript call-by-ref将一个对象复制到另一个对象中,但在我的项目中,我需要call-by-value.将一个对象复制到另一个对象中call-by-value.

    var query=domain.User.find({
            deleted: false,
            role: role
        })

var query1=query;

I have the scenario change in the query object is not reflected in query1 .我有查询对象中的场景更改未反映在query1 I google and try so many way to clone the object but it does't work.The query object is used in another function for pagination and query1 object is used for count query.我谷歌并尝试了很多方法来克隆对象,但它不起作用。查询对象在另一个函数中用于分页,而query1对象用于计数查询。

1.I used to Object.clone(query1) error Object.clone is not function 2.I used Object.assign(query1) but it does't works fine. 1.我习惯于 Object.clone(query1) 错误 Object.clone is not function 2.I used Object.assign(query1) 但它不能正常工作。 3.I used other so many ways can anybody help me to sort this problem 3.我用了很多其他方法谁能帮我解决这个问题

you are trying to clone a cursor, but it is not the right approach, you probably just need to create another您正在尝试克隆游标,但这不是正确的方法,您可能只需要创建另一个

like this:像这样:

var buildQuery = function() {
  return domain.User.find({
    deleted: false,
    role: role
  });
};

var query = buildQuery();
var query1 = buildQuery();

Alternative solution using merge method:使用merge方法的替代解决方案:

const query = domain.User.find({
  deleted: false,
  role: role
}).skip(10).limit(10)

const countQuery = query.model.find().merge(query).skip(0).limit(0)

const [users, count] = await Promise.all([query, countQuery.count()])

This is work for me:这对我来说是工作:

const qc = sourceQuery.toConstructor();
const clonedQuery = new qc();

This code work in pagination function where sourceQuery passed as parameter and i dont known what models used.这段代码在 sourceQuery 作为参数传递的分页函数中工作,我不知道使用了什么模型。 Also it work with aggregations and complex queries.它还适用于聚合和复杂查询。

  public async paging(
    query: mongoose.DocumentQuery<mongoose.Document[], mongoose.Document>,
    params,
    transformer: any = null
  ) {
    let page = Number(params.page);
    if (!page) page = 1;

    let page_size = Number(params.count);
    if (!page_size) page_size = 100;

    const qc = query.toConstructor();
    const cq = new qc();
    return cq.countDocuments().exec()
      .then(async (total) => {
        const s = params.sort;
        if (s) {
          query.sort(s);
        }
        query.limit(page_size);
        query.skip(page_size * (page - 1));
        let results = await query.exec();
        if (transformer) {
          results = await Promise.all(results.map((i) => transformer(i)));
        }
        const r = new DtoCollection();
        r.pages = Math.ceil(total / page_size);
        r.total = total;
        (r.results as any) = results;
        return r;
      });
  }

Sergii Stotskyi's answer works just fine and is very elegant, except that count is deprecated. Sergii Stotskyi 的回答工作得很好并且非常优雅,只是不推荐使用count

countDocuments or estimatedDocumentCount should be used instead.应使用countDocumentsestimatedDocumentCount来代替。

However, this causes the error the limit must be positive .但是,这会导致错误the limit must be positive We can walk around this by set limit to a large integer.我们可以通过将limit设置为一个大整数来解决这个问题。

const query = domain.User.find({
  deleted: false,
  role: role
}).skip(10).limit(10)

const countQuery = query.model.find().merge(query).skip(0).limit(Number.MAX_SAFE_INTEGER)

const [users, count] = await Promise.all([query, countQuery.countDocuments()])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM