Skip to content

indexing doesn't work with object fields #83

@xh-lin

Description

@xh-lin

After indexing a field that's an object type, the value will become empty after inserting.
I cannot use objects for the _id field because of the same issue
I believe the issue is maybe in this function

/**
* Type-aware projection
*/
function projectForUnique(elt) {
if (elt === null || Array.isArray(elt)) {
return '$null';
}
if (typeof elt === 'string') {
return '$string' + elt;
}
if (typeof elt === 'boolean') {
return '$boolean' + elt;
}
if (typeof elt === 'number') {
return '$number' + elt;
}
if (util.isArray(elt)) {
return '$date' + elt.getTime();
}
return elt; // Arrays and objects, will check for pointer equality
}

export function testIndex(): void {
  db.loadDatabase();
  db.remove({}, { multi: true });

  db.ensureIndex({ fieldName: 'key' }, (err) => {
    if (err != null) LOG.error('ensureIndex err:', err);
  });

  db.insert({ key: { a: 1 } }, (err, doc) => {
    if (err != null) LOG.error('a1:', err);
    else LOG.info('a1 doc added:', doc);
  });
  db.insert({ key: { a: 2 } }, (err, doc) => {
    if (err != null) LOG.error('a2:', err);
    else LOG.info('a2 doc added:', doc);
  });
  db.insert({ key: { b: 1 } }, (err, doc) => {
    if (err != null) LOG.error('b1:', err);
    else LOG.info('b1 doc added:', doc);
  });
  db.insert({ key: { b: 2 } }, (err, doc) => {
    if (err != null) LOG.error('b2:', err);
    else LOG.info('b2 doc added:', doc);
  });

  db.count({}, (_, count) => {
    LOG.info(`count: ${count}`);
  });
}

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions