search

Home  >  Q&A  >  body text

显示某条记录的某个字段(node.js+mongodb)

怎么显示某条记录的某个字段(node.js+mongodb)?

伊谢尔伦伊谢尔伦2754 days ago631

reply all(3)I'll reply

  • 为情所困

    为情所困2017-05-02 09:28:23

    You should be able to use mongoose

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-02 09:28:23

    //使用mongoose
    model.findOne({ type: 'iphone' }, 'name', function (err, doc) {}); 
    或者
     model.findOne({ type: 'iphone' }, 'name').exec(); 
    

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-02 09:28:23

    You might as well take a look at the document: Collection#find (the following code is quoted from the document)

    // A simple query showing skip and limit
    var MongoClient = require('mongodb').MongoClient,
      test = require('assert');
    MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    
      // Create a collection we want to drop later
      var collection = db.collection('simple_limit_skip_query');
      // Insert a bunch of documents for the testing
      collection.insertMany([{a:1, b:1}, {a:2, b:2}, {a:3, b:3}], {w:1}, function(err, result) {
        test.equal(null, err);
    
        // Perform a simple find and return all the documents
        collection.find({})
          .skip(1).limit(1).project({b:1}).toArray(function(err, docs) {
            test.equal(null, err);
            test.equal(1, docs.length);
            test.equal(null, docs[0].a);
            test.equal(2, docs[0].b);
    
            db.close();
        });
      });
    });

    Pay attention to the find part, it should be what you want.

    reply
    0
  • Cancelreply