javascript基本的mongoose命令(代码片段)

author author     2022-12-07     151

关键词:

'use strict';

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

const  DATABASE_URL  = require('./config');

mongoose.connect(DATABASE_URL,  useMongoClient: true );

const favoriteSchema = mongoose.Schema(
  name: 
    type: String,
    unique: true,
    required: true
  ,
  votes:  type: Number, default: 0 ,
  comment: String,
);

const Favorite = mongoose.model('Favorite', favoriteSchema);

Favorite.create(
  name: 'The Hitchhiker’s Guide to the Galaxy',
  comment: 'I won’t enjoy it. –Marvin',
  votes: 42
).then(favorite => 
  console.log('Created favorite:', favorite);
);

Favorite.create(
  name: 'Serenity',
  comment: 'Great Movie',
  votes: 99
).then(favorite => 
  console.log('Created favorite:', favorite);
);

Favorite.find()
  .then(favorites => 
    console.log('Read favorites:', favorites);
  );

Favorite.findById('5a1d82fabbed51aa01614c31')
  .then(favorites => 
    console.log('Read favorites:', favorites);
  );

Favorite.findOne(
  name: 'The Hitchhiker’s Guide to the Galaxy'
).then(favorites => 
  console.log('Read favorites:', favorites);
);

Favorite.updateOne( name: 'Serenity' ,  votes: 42 )
  .then(result => 
    console.log('Write Results', result);
  );

Favorite.updateMany( votes: 42 ,  votes: 99 )
  .then(favorite => 
    console.log('Updated favorite', favorite);
  );

Favorite.findByIdAndUpdate('5a1d8b9a9298d8b1101e6f06',
   name: 'Upserted document', comment: 'this is new', votes: 52 ,
   new: true, upsert: true 
).then(favorite => 
  console.log('Updated favorite', favorite);
);


Favorite.findByIdAndRemove('5a1d8b9a9298d8b1101e6f08')
  .then((res) => 
    if (res) 
      console.log('Deleted', res);
     else 
      console.log('Id not found, so can not delete');
    
  );

Favorite.remove().then((results) => 
  console.log(results);
);

基本节点/mongo/mongoose 连接不起作用

】基本节点/mongo/mongoose连接不起作用【英文标题】:Basicnode/mongo/mongooseconnectionnotworking【发布时间】:2015-01-1811:42:16【问题描述】:我是MEAN堆栈开发的新手。我在MongoDB中创建了一个名为“framework”的数据库和一个名为“users”的... 查看详情

javascript垃圾回收机制

JavaScript垃圾回收机制1垃圾为何要产生并回收2垃圾回收机制2.1标记清除法2.2引用计数法3V8对垃圾回收机制的优化——分代式垃圾回收机制3.1新生代与老生代3.2新生代的垃圾回收3.3老生代的垃圾回收1垃圾为何要产生并回收当我们... 查看详情

为啥 Mongoose 的 JavaScript 在同一行同时有返回和回调?

】为啥Mongoose的JavaScript在同一行同时有返回和回调?【英文标题】:WhydoesMongoose\'sJavaScripthavebothareturnandacallbackinthesameline?为什么Mongoose的JavaScript在同一行同时有返回和回调?【发布时间】:2016-01-0512:39:03【问题描述】:我正在学... 查看详情

mongoose 查询返回纯 javascript

】mongoose查询返回纯javascript【英文标题】:mongoosequeryreturnsplainjavascript【发布时间】:2018-06-2019:16:08【问题描述】:我对猫鼬有疑问mongoose查询不返回mongoose文档实例这是我的架构:constmongoose=require(\'mongoose\');varAlbumSchema=newmongoose.... 查看详情

node----deprecationwarning:currenturlstringparserisdeprecated和port3000isalreadyinuse的问题(代(代码片段)

...问题的解决办法:(1)URL字符串解析问题的解决办法:mongoose连接数据库时除了url参数外增加2个参数,如下所示:mongoose.connect("mongodb://127.0.0.1:27017/test",useNewUrlParser:true,function(err)  if(err)    console.log(‘ConnectionError:‘+err)... 查看详情

Mongoose 添加到 MongoDB 的基本功能?

】Mongoose添加到MongoDB的基本功能?【英文标题】:BasicfeaturesthatMongooseaddedtoMongoDB?【发布时间】:2017-11-1219:07:58【问题描述】:Mongoose为MongoDB添加了哪些基本功能?我想明确知道他们的功能。【问题讨论】:【参考方案1】:MongoDB... 查看详情

使用 javascript 数组方法更新 mongoose 中的嵌套数组

】使用javascript数组方法更新mongoose中的嵌套数组【英文标题】:Updateanestedarrayinmongooseusingjavascriptarraymethods【发布时间】:2020-07-2603:30:39【问题描述】:我在更新mongoose架构中的mixedtype字段时遇到问题。这不是最好的架构设置但我... 查看详情

javascript[猫鼬片段]猫鼬#mongoose的提示(代码片段)

查看详情

安装使用mongoose配合node.js操作mongodb的基础教程转载

...础教程,前端js+后端node+js操作MongoDB正是所谓最流行的一种JavaScript全栈开发方案,需要的朋友可以参考下安装mongoose使用express准备一个TestMongoDB项目,命令序列如下:expressTestMongoDBcdTestMongoDBnpminstall执行完上面的命令后, 查看详情

javascript用于nodejs的sql中的一些基本c.r.u.d命令。仅供将来参考。(代码片段)

查看详情

javascript如何使用mongoose删除mongodb中的集合?(代码片段)

查看详情

javascript使用nodejs和mongoose的mongodbcrud操作(代码片段)

查看详情

javascript的基本语法

 【JavaScript的基本语法】1.javascript输出JavaScript语句向浏览器发出的命令。语句的作用是告诉浏览器该做什么。<script>   document.write("helloworld!");</script>插入,输出。document.getElementById("pid").innerHTML=" 查看详情

在 Mongoose 查询中使用 javascript Promise

】在Mongoose查询中使用javascriptPromise【英文标题】:UsingjavascriptpromiseswithMongoosequeries【发布时间】:2018-09-0915:18:12【问题描述】:通常,如果我要运行多个mongoose查询,我会使用内置的Promise将它们链接在一起。在这种情况下,用... 查看详情

Mongoose/Mongodb 类似 trello 的基本方案问题,在 vue 中呈现

】Mongoose/Mongodb类似trello的基本方案问题,在vue中呈现【英文标题】:Mongoose/Mongodbbasictrellolikeschemeproblemwithrenderinginvue【发布时间】:2019-06-3004:55:50【问题描述】:我正在创建一个非常基本的功能看板。到目前为止,我的板子有4... 查看详情

基本的 graphQL Mongoose 设置

】基本的graphQLMongoose设置【英文标题】:BasicgraphQLMongoosesetup【发布时间】:2016-10-1118:12:48【问题描述】:我关注了http://docs.apollostack.com/apollo-server/guide.html/和这个video,并想尝试将其连接到www.mlab.com上的db。该项目正在连接到mlab... 查看详情

mongoose一看就会的基本操作(代码片段)

Mongoose是在node.js异步环境下对mongodb进行便捷操作的对象模型工具  那么要使用它,首先你得装上node.js和mongodb,关于mongodb的安装和操作介绍可以参考:http://www.cnblogs.com/zhongweiv/p/node_mongodb.html   Github地址:https://github.com/Au... 查看详情

使用mongoose模块向本地mongodb数据库中插入数据报错“operation`people.insertone()`bufferingtimedoutafter10000ms“(代(代码片段)

异常连接本地的mongodb数据库报错:MongooseError:Operation`people.insertOne()`bufferingtimedoutafter10000msatTimeout.<anonymous>(D:\\NodeJs\\node-demo\\node_modules\\mongoose\\lib\\drivers\\node 查看详情