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

浏览:95日期:2023-06-15

问题描述

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

问题解答

回答1:

使用mongoose应该可以吧

回答2:

//使用mongoosemodel.findOne({ type: ’iphone’ }, ’name’, function (err, doc) {}); 或者 model.findOne({ type: ’iphone’ }, ’name’).exec(); 回答3:

不妨看下文档:Collection#find(以下代码引用自文档)

// A simple query showing skip and limitvar 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(); }); });});

注意find部分,应该就是你要的内容。

相关文章: