Nodejs judges the type of MongoDB collection _id

background:

  1. In MongoDB, the _id field is the primary key representing a collection, which uniquely identifies each document.

  1. Use default _id: By default, when inserting a document in a collection, if there is no field name for _id in the field name, MongoDB will automatically add an ObjectId field.

  1. Custom _id field: When we add the "_id" field displayed by the document to a collection, MongoDB will not generate the _id field by default, but use the "_id" field we specified.

  1. When we use MongoDB, we use the above two ways to generate _id. For documents that need to use a unique ID, we generate it ourselves, otherwise we use the _id automatically generated by MongoDB.

Problems encountered:

  1. Because our _id has two generation methods, when using nodejs to operate MongoDB, when deleting and updating, it cannot be updated and deleted due to the problem of the _id field type

  1. If the collection uses the _id automatically generated by MongoDB, it must be written like this when operating: {_id: new ObjectID(_id)}

How to distinguish the type of _id field?

  1. The _id generated by ourselves is of string type, while the _id automatically generated by MongoDB is of ObjectID type. How to judge the type of _id?

2. I originally wanted to judge the type of _id through the collection API of MongoDB, but after searching for a long time, I still couldn’t find the corresponding API (if you know, please let me know, thank you)

3. Since there is no corresponding API on the collection, the type of _id can only be judged through the query results. The following is my judgment logic:

  var url = 'mongodb://' + username + ':' + password+'@' + ip + ":"+port + "/" + dbName;
  let client = new MongoClient(url);
  await client.connect();
  let db = client.db(dbName);
  let collection = db.collection(collectionName);
  let data = await collection.find(JSON.parse(filter)).sort(JSON.parse(sort)).limit(limit).skip(skip).toArray();
  let result = new Object();
  result.isObjectId = false;
  if (data.length !== 0) {
    if ('[object Object]' === Object.prototype.toString.call(data[0]._id)) {
      result.isObjectId = true;
    }
  }
  result.data = data;
  return result;
  1. Update docs:

  var url = 'mongodb://' + username + ':' + password+'@' + ip + ":"+port + "/" + dbName;
  let client = new MongoClient(url);
  await client.connect();
  let db = client.db(dbName);
  let collection = db.collection(collectionName);
  if (isObjectId) {
      return collection.replaceOne({_id: new ObjectID(filter)}, doc);
    } 
  return collection.replaceOne({_id: filter}, doc);
  1. Delete document:

  var url = 'mongodb://' + username + ':' + password+'@' + ip + ":"+port + "/" + dbName;
  let client = new MongoClient(url);
  await client.connect();
  let db = client.db(dbName);
  let collection = db.collection(collectionName);
  if (isObjectId) {
    return collection.deleteOne({_id: new ObjectID(filter)}); 
  }
  return collection.deleteOne({_id: filter}); 

Summarize:

Record the _id type of each document currently queried, and then generate the corresponding _id type according to the _id type when updating.

Guess you like

Origin blog.csdn.net/duzm200542901104/article/details/129503743