mongodb

Add data inside documents in Mongo DB using PHP

微笑、不失礼 提交于 2021-02-18 22:40:30
问题 I want to insert data in Mongo database using PHP script, in year wise documents so that it may look like this (All years in one document); cars{ 2017{ car=Motorolla color = blue } 2016{ car=Toyota color = green } 2015{ car=Corolla color = black } } I wanted to add the document but it prompts Document can't have $ prefixed field names: $years[0] Is it possible to make such schema in Mongo using PHP? Code <?php try { $car = 'Motorolla'; $color = 'blue'; //$car = 'Toyota'; //$color = 'green'; /

Mongodb find query with $near and coordinates not working

…衆ロ難τιáo~ 提交于 2021-02-18 22:21:07
问题 I'm trying to make use of some geolocation functionality in mongodb. Using a find query with $near doesn't seem to work! I currently have this object in my database: { "Username": "Deano", "_id": { "$oid": "533f0b722ad3a8d39b6213c3" }, "location": { "type": "Point", "coordinates": [ 51.50998, -0.1337 ] } } I have the following index set up as well: { "v": 1, "key": { "location": "2dsphere" }, "ns": "heroku_app23672911.catchmerequests", "name": "location_2dsphere", "background": true } When I

Get ID of affected document by using update()

女生的网名这么多〃 提交于 2021-02-18 20:59:53
问题 I'm using this for doing an upsert: Articles.update( { title: title }, { title: title, parent: type }, { upsert: true }, function(res) { return console.log(res); } ); console.log(needToGetID); Now I need to get the _id of the document which has been updated or inserted. I thought I can get that via callback, but res is undefined. I assume there is just one unique document which is defined by the query. Update Forgot to mention that I'm using meteor... 回答1: The intent of .update() is to

How to delete the last item of a collection in mongodb

这一生的挚爱 提交于 2021-02-18 19:32:27
问题 I made a program with python and mongodb to do some diaries. Like this Sometimes I want to delete the last sentence, just by typing "delete!" But I dont know how to delete in a samrt way. I dont want to use "skip". Is there a good way to do it? 回答1: Be it first or last item, MongoDB maintains unique _id key for each record and thus you can just pass that id field in your delete query either using deleteOne() or deleteMany() . Since only one record to delete you need to use deleteOne() like db

Clean up dead references with Mongoose populate()

别说谁变了你拦得住时间么 提交于 2021-02-18 18:59:49
问题 If a user has an array called "tags": var User = new Schema({ email: { type: String, unique: true, required: true }, tags: [{ type: mongoose.Schema.Types.ObjectId, ref:'Tag', required: true }], created: { type: Date, default: Date.now } }); and I do a populate('tags') on a query: User.findById(req.params.id) .populate("tags") .exec(function(err, user) { ... }); If one of the tags in the list has actually been deleted, is there a way to remove this dead reference in "tags"? Currently, the

Clean up dead references with Mongoose populate()

≡放荡痞女 提交于 2021-02-18 18:59:32
问题 If a user has an array called "tags": var User = new Schema({ email: { type: String, unique: true, required: true }, tags: [{ type: mongoose.Schema.Types.ObjectId, ref:'Tag', required: true }], created: { type: Date, default: Date.now } }); and I do a populate('tags') on a query: User.findById(req.params.id) .populate("tags") .exec(function(err, user) { ... }); If one of the tags in the list has actually been deleted, is there a way to remove this dead reference in "tags"? Currently, the

MongoDB Cloud Deployment - High TTFB

落爺英雄遲暮 提交于 2021-02-18 18:51:50
问题 I am building a mobile only application in Node.js + MongoDB. I have deployed my server in the AWS AP-Southeast-1 Region . Since I am new to MongoDB, I am leveraging cloud hosting services like MongoLabs, Compose.IO, MongoDirector (testing a few out). Now, these cloud hosting platforms are deploying my database in either of the AWS AP-Southeast-2 OR US-East-1 Region due to unavailability of Shared hosting in the Southeast-1 Region. While testing my APIs, I saw a alarmingly high latency in the

MongoDB Cloud Deployment - High TTFB

风流意气都作罢 提交于 2021-02-18 18:51:36
问题 I am building a mobile only application in Node.js + MongoDB. I have deployed my server in the AWS AP-Southeast-1 Region . Since I am new to MongoDB, I am leveraging cloud hosting services like MongoLabs, Compose.IO, MongoDirector (testing a few out). Now, these cloud hosting platforms are deploying my database in either of the AWS AP-Southeast-2 OR US-East-1 Region due to unavailability of Shared hosting in the Southeast-1 Region. While testing my APIs, I saw a alarmingly high latency in the

MongoDB增删改查

前提是你 提交于 2021-02-18 17:40:28
一:启动mongodb 1、linux系统(Ubuntu):sudo server mongodb start 2、window系统:mongod,启动之后不能关闭cmd窗口,必须挂起 二:运行mongodb 命令:mongo 三、创建数据库 使用use 命令创建数据库----有就切换,没有就创建数据库 命令:use 数据库名 切换数据库 命令:use 数据库名 eg: 查看当前数据库: 查看所有数据库 销毁数据库 四:创建集合 在 MongoDB 中,一个数据库包含多个集合,类似于 MySQL 中一个数据库包含多个表;一个集合包含多个文档,类似于 MySQL 中一个表包含多条数据。 可以把集合记为表,文档记为一条记录。 命令:db.createcolleciton("school") 查看所有集合: 命令:show collections 五:插入数据 插入数据有两个命令 命令:db.school.insert() 命令:db.school.save() insert 和 save 的区别:为了方便记忆,可以先从字面上进行理解,insert 是插入,侧重于新增一个记录的含义;save 是保存,可以保存一个新的记录,也可以保存对一个记录的修改。因此,insert 不能插入一条已经存在的记录,如果已经有了一条记录(以主键为准),insert 操作会报错,而使用 save

How can I coax Spring Data to show me mongo's query plan (a.k.a cursor.explain())

ぃ、小莉子 提交于 2021-02-18 15:34:33
问题 I am writing an API with Spring/Mongo/Jersey to do CRUD on a POJO that has a generic map of properties like this: public class Thing { private String id; @Indexed private Map<String,String> properties; ... This is working great to return items. My resource code looks like this: BasicDBObject query = new BasicDBObject("properties.name", "vlad the impaler"); return Response.ok(myService.queryThings(query)).build(); And my abstract DAO looks like this: public List<T> find(Query query) { return