antoniopresto / react-native-local-mongodb

react-native local mongodb storage.
MIT License
304 stars 66 forks source link

indexing doesn't work with object fields #83

Open xh-lin opened 6 months ago

xh-lin commented 6 months ago

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 https://github.com/antoniopresto/react-native-local-mongodb/blob/b5955beba169fe636c042dab1a0f0e74cfecd1de/lib/indexes.js#L13-L34

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