use mongoose to update documents with model and Schema
2014-01-17 09:20
381 查看
Problem
定义Schema 和Model,在Schema定义了嵌套的数据结构,例如:
然后获取model
var Adapter = new Schema({entity: []})
那么将来更新了 adapter,并使用 Model进行save或update时,entities一直没变化。
就是说,没有办法更新数据库记录。
Solution
So.1
http://stackoverflow.com/questions/8242601/node-mongoose-problems-saving-multiple-depths-of-nested-schema
如果是定义多层的数据,嵌套。
那么就需要声明多个schema
例如
So.2
http://mongoosejs.com/docs/schematypes.html
use mixed type
Get it done
定义Schema 和Model,在Schema定义了嵌套的数据结构,例如:
然后获取model
var Adapter = new Schema({entity: []})
那么将来更新了 adapter,并使用 Model进行save或update时,entities一直没变化。
就是说,没有办法更新数据库记录。
Solution
So.1
http://stackoverflow.com/questions/8242601/node-mongoose-problems-saving-multiple-depths-of-nested-schema
如果是定义多层的数据,嵌套。
那么就需要声明多个schema
例如
var Entity = new Schema({ foo:String, bar:String }) var Adapter = new Schema({ entity: [Entity] })
So.2
http://mongoosejs.com/docs/schematypes.html
use mixed type
var schema = new Schema({ name: String, binary: Buffer, living: Boolean, updated: { type: Date, default: Date.now } age: { type: Number, min: 18, max: 65 } mixed: Schema.Types.Mixed, _someId: Schema.Types.ObjectId, array: [], ofString: [String], ofNumber: [Number], ofDates: [Date], ofBuffer: [Buffer], ofBoolean: [Boolean], ofMixed: [Schema.Types.Mixed], ofObjectId: [Schema.Types.ObjectId], nested: { stuff: { type: String, lowercase: true, trim: true } } }) // example use var Thing = mongoose.model('Thing', schema); var m = new Thing; m.name = 'Statue of Liberty' m.age = 125; m.updated = new Date; m.binary = new Buffer(0); m.living = false; m.mixed = {[ any: { thing: 'i want' } ]}; m.markModified('mixed'); m._someId = new mongoose.Types.ObjectId; m.array.push(1); m.ofString.push("strings!"); m.ofNumber.unshift(1,2,3,4); m.ofDate.addToSet(new Date); m.ofBuffer.pop(); m.ofMixed = [1, [], 'three', { four: 5 }]; m.nested.stuff = 'good'; m.save(callback);
Get it done
相关文章推荐
- [Android] Use Jsoup to grab the web data and process the data with string.indexOf()
- How to use UTF-8_with_BOM, XML and Java together
- eclipse中,项目有红叉之-Project configuration is not up-to-date with pom.xml. Select: Maven->Update Project... from the project context menu or use Quick Fix
- How To Read and Write XML Documents with GDataXML
- How and Why to use the System.servicemodel.MessageParameterAttribute in WCF
- Why And When To Use Pre-Update and Pre-Insert Triggers In Oracle Forms
- Cannot update paths and switch to branch 'snapshot_mixuse' at the same time.
- Enabling HDFS/YARN HA and Other CDH Components to Use HDFS HA with Cloudera Manager 5.2
- Maven异常Type Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix
- Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix.
- How to use Comparator and Comparable in Java? With example
- Tip/Trick: Cool UI Templating Technique to use with ASP.NET AJAX for non-UpdatePanel scenarios
- how to use a SQLite database in a standalone program with an HTML interface and VBScript as the programming language
- 2016.7.12 Table configuration with catalog null, schema public, and table globalpage did not resolve to any tables(疑)
- How to update WPF browser application manifest and xbap file with ‘mage.exe’
- Database Patch Set Update Overlay Patches Required for Use with PSUs and Oracle E-Business Suite
- How to create fully custom Role, User, Event, Resource classes for use with the Security and Scheduler modules
- How to Use C++ With PHP and JQuery to Make Neat Apps
- Use ANT and Maven to create executable jar with dependent library
- PluggableSchemaResolver:106 - Trying to resolve XML entity with public id [null] and system id