mongodb

mongoose do not return saved document in callback

徘徊边缘 提交于 2021-02-08 09:52:56
问题 I am trying to save a new record using mongoose. I am not getting the saved document in the callback. app.post("/register",(req,res) => { let userData = req.body; let user = new User(userData) user.save().then((err,doc) => { res.json({"success":true,"data":doc}); console.log(doc); }) }); I am getting doc:1. While I should get the whole document. Please help me. "dependencies": { "body-parser": "^1.18.2", "crypto-js": "^3.1.9-1", "express": "^4.15.5", "mongoose": "^4.11.13" } 回答1: You're using

MongoDB aggregation over a range

為{幸葍}努か 提交于 2021-02-08 09:12:30
问题 I have documents of the following format: [ { date:"2014-07-07", value: 20 }, { date:"2014-07-08", value: 29 }, { date:"2014-07-09", value: 24 }, { date:"2014-07-10", value: 21 } ] I want to run an aggregation query that gives me results in date ranges. for example [ { sum: 49 }, { sum:45 }, ] So these are daily values, I need to know the sum of value field for last 7 days. and 7 days before these. for example sum from May 1 to May 6 and then sum from May 7 to May 14. Can I use aggregation

MongoDB aborting when running mongod command on terminal

半世苍凉 提交于 2021-02-08 09:10:38
问题 when i run mongod command on ubuntu terminal i get this error: {"t":{"$date":"2020-10-28T22:27:29.341+05:00"},"s":"I", "c":"CONTROL", "id":23285, "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"} {"t":{"$date":"2020-10-28T22:27:29.348+05:00"},"s":"W", "c":"ASIO", "id":22601, "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"} {"t":{"$date":"2020-10-28T22:27:29.349+05:00"},"s":"I", "c":"NETWORK",

Node.js + Mongoose / Mongo & a shortened _id field

这一生的挚爱 提交于 2021-02-08 08:52:29
问题 I'd like the unique _id field in one of my models to be relatively short: 8 letters/numbers, instead of the usual Mongo _id which is much longer. Having a short unique-index like this helps elsewhere in my code, for reasons I'll skip over here. I've successfully created a schema that does the trick (randomString is a function that generates a string of the given length): new Schema('Activities', { '_id': { type: String, unique: true, 'default': function(){ return randomString(8); } }, // ...

How project DBRef on Spring MongoDB Aggregation?

 ̄綄美尐妖づ 提交于 2021-02-08 08:45:20
问题 I have the following aggregation done in a MongoDB shell to get the number of alerts of each type for each user: db.getCollection('alerts').aggregate( { $unwind:"$son" }, { $group: { _id:{ son: "$son", level: "$level" }, count: { $sum: 1 } } }, { $group: { _id:{ son: "$_id.son" }, alerts: { $addToSet: { level: "$_id.level", count: "$count" }} } } ) I have translated it to Spring Data MongoDB as follows: TypedAggregation<AlertEntity> alertsAggregation = Aggregation.newAggregation(AlertEntity

Nodejs Follow System Not Working Properly

匆匆过客 提交于 2021-02-08 08:42:09
问题 I have a follow system app.get('/user/:id', function(req, res){ User.findById(req.user ).exec(function(err, user){ User.findById(req.params.id, function(err, kullanici){ User.findById(user, {follower:kullanici._id}, function(err, follow){ if(follow== ""+kullanici._id+""){ console.log("sdasdsad") }else{ console.log("no") } ... kullanici = user in profile user = current user follow =the users who follows the kullanici When current user is one of the takip then I will console log sdasdsad but

How to Flatten dynamic field with parent document - Spring data Mongo DB

巧了我就是萌 提交于 2021-02-08 08:26:22
问题 In my Spring boot project have a Document like so: @Document(collection="AuditTable") public class AuditTable { @Id private String id; private Map<String, String> properties; where properties is a dynamic field i.e. it can take in as many different key-value pairs. I use MongoRepository to store this value: @Repository public interface AuditTableRepo extends MongoRepository<AuditTable, String> { } Now when I store it in the Collection it looks like this: whereas I want it to look like this: "

Mongoose Compound Index Unique + Sparse

落花浮王杯 提交于 2021-02-08 08:16:44
问题 I want to create an index which ensures, that I don't have a duplicate serialNr within the combination of a manufacturer + art . But some items don't have a serialNr . These I don't want to check/index. Code mySchema.index({ serialNr: 1, art: 1 , manufacturer: 1, deleted: 1}, { unique: true, sparse: true) I tried it also with adding a partial Filter: partialFilterExpression: { serialNr: {$ne:null} } to the index options. Question How can I index it that inserting: [{art: 'a', manufacturer:'a'

How to clone a Mongodb database with Mongoose

泪湿孤枕 提交于 2021-02-08 07:56:34
问题 Is there a way to clone a collection or entire Mongodb database (my databases only have one collection so both options are OK) with Mongoose? I saw that there is a possibility to execute raw Mongo commands with Mongoose. What command can I use to clone an entire collection or db from one db to another? Thanks in advance. 回答1: I had a hard time doing this I don't have any reference. However, this is how I did on my end. 1, I created another collection within the same db: mydb collections:

Adding values to an Array in MongoDB with Java

旧巷老猫 提交于 2021-02-08 07:55:00
问题 I have a document stored in a collection in a mongo database. I want to be able to add to two arrays that are already in the document. Method for creating the document and arrays: public void addNewListName(String listName) { MongoCollection<Document> collection = database.getCollection("lists"); ArrayList< DBObject > array = new ArrayList< DBObject >(); Document list = new Document ("name", listName) .append("terms", array) .append("definitions", array); collection.insertOne(list); } Method