Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indexing doesn't work with object fields #83

Open
xh-lin opened this issue May 11, 2024 · 0 comments
Open

indexing doesn't work with object fields #83

xh-lin opened this issue May 11, 2024 · 0 comments

Comments

@xh-lin
Copy link

xh-lin commented May 11, 2024

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

@xh-lin xh-lin changed the title index doesn't work for object fields index doesn't work with object fields May 11, 2024
@xh-lin xh-lin changed the title index doesn't work with object fields indexing doesn't work with object fields May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant