I have below json data and want to update the value according to the condition
{
"_id" : ObjectId("5fce2d4c7b2ea2e79ercvju4"),
"userId" : 1,
"token": "jwt_token_value",
"isActive" : 0,
"__v" : 0
}
{
"_id" : ObjectId("5fce2d4c7b2ea2e79ercvjk0"),
"userId" : 1,
"token": "jwt_token_value",
"isActive" : 0,
"__v" : 0
}
{
"_id" : ObjectId("5fd0d45tjd82a02dd0f17fc4"),
"userId" : 1,
"token": "jwt_token_value",
"isActive" : 1,
"__v" : 0
}
I have managed the things as below.
let update = await UserDetails.findAndModify(
{ userId: 1, token: token },
[],
{$set: { isActive: 0 }},
{ new: true }
);
=> This query should update the last json collection key of isActive to 1. But it is not updating the values any how. What the things that I am doing wrong ? It is not throwing me any error as well to debug.
I am following this answer: https://stackoverflow.com/a/24648693/5624578
You can include the query identifiers to specify what each section is doing like this:
let update = await UserDetails.findAndModify(
query: { userId: 1, token: token },
update: {$set: { isActive: 0 }},
upsert: true
);
Related
I am trying to use Mongoose findByIdAndDelete() method on a model but it does not see the _id it needs to delete.
My route is declared similar to this (minified) example:
router.delete('/delete-entity/:id', (req, res) => {
Entity.findByIdAndDelete(req.params.id)
.exec()
.then((docs) => { ...handle success })
.catch((err) => { ...handle error });
});
I only have middleware running that hooks into the save method of the model. But this middleware does not touch the _id field.
When I console.log() inside the route I can see that req.params.id is set. Also if I try to identify my document using an other field to delete it (so not the _id field), it does works. However when I try to use the _id field all that is returned to the client is ''. This makes me believe that something goes wrong in comparing datatypes of the _id field. But cating using mongoose.Type.ObjectId() did not help either.
What could be going on here?
In addition to this already provided information. Here is my model.
const embeddedEntity = new mongoose.Schema({
name: { type: String, required: true }
}, { _id: false });
const entity = new mongoose.Schema({
name: { type: String, required: true },
embeddedInfo: { type: embeddedEntity, required: true }
});
module.exports = mongoose.model('Entity', entity);
#Invider I managed to get this from the profiler.
{
"op" : "command",
"ns" : "development.entities",
"command" : {
"findAndModify" : "entities",
"query" : {
"_id" : ObjectId("5dc95b8cc472d31232dba5a3")
},
"new" : false,
"remove" : true,
"upsert" : false,
"lsid" : {
"id" : UUID("30086660-0619-440e-9268-148957428a2b")
},
"$db" : "development"
},
"keysExamined" : 0,
"docsExamined" : 0,
"ndeleted" : 0,
"numYield" : 0,
"locks" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(1),
"w" : NumberLong(1)
}
},
"Database" : {
"acquireCount" : {
"w" : NumberLong(1)
}
},
"Collection" : {
"acquireCount" : {
"w" : NumberLong(1)
}
}
},
"responseLength" : 74,
"protocol" : "op_msg",
"millis" : 0,
"planSummary" : "IDHACK",
"execStats" : {
"stage" : "DELETE",
"nReturned" : 0,
"executionTimeMillisEstimate" : 0,
"works" : 1,
"advanced" : 0,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"nWouldDelete" : 0,
"nInvalidateSkips" : 0,
"inputStage" : {
"stage" : "IDHACK",
"nReturned" : 0,
"executionTimeMillisEstimate" : 0,
"works" : 1,
"advanced" : 0,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keysExamined" : 0,
"docsExamined" : 0
}
},
"ts" : ISODate("2019-11-14T09:27:52.988Z"),
"client" : "127.0.0.1",
"allUsers" : [ ],
"user" : ""
}
A lot of thanks goes out to #invider and #SuleymanSah for pointing me into the right direction.
The problem was actually something pretty simple that I completely overlooked and was indeed related to the datatype as I expected.
My e2e tests were failing because of the same problem as trying to call the route using Postman.
The problem was that when adding test samples to the database I was setting the _id field myself. This most likely causes the value stored to be of type string. Letting MongoDB itself set the _id field solved this issue. At least for working in the live environment using Postman.
For my e2e test I had to modify my the data that will be loaded into the database for each test. I was adding samples in the following way:
{
_id: '5dc95b8cc472d31232dba5a5',
name: 'SomeData',
embeddedInfo: {
name: 'someOtherData'
}
}
I should have casted the _id into an ObjectId first. Like so:
{
_id: mongoose.Types.ObjectId('5dc95b8cc472d31232dba5a5'),
name: 'SomeData',
embeddedInfo: {
name: 'someOtherData'
}
}
Again. Thanks to the both of you.
I have this data in MongoDB:
{
"_id" : ObjectId("5c7e459f875ea5548de25722"),
"Autos" : [
{
"_id" : ObjectId("5cad9759e1c3895999adaceb"),
"deleted" : 1,
},
{
"_id" : ObjectId("5cad9a8be1c3895999adacef"),
"deleted" : 0,
},
{
"_id" : ObjectId("5cad9aa4e1c3895999adacf0"),
"deleted" : 0,
}
]
}
{
"_id" : ObjectId("5c7e45e9875ea5548de25724"),
"Shoemaking" : [
{
"_id" : ObjectId("5cad9770e1c3895999adacec"),
"deleted" : 1,
},
{
"_id" : ObjectId("5cad9a5de1c3895999adaced"),
"deleted" : 0,
},
]
I want to basically select * from table where deleted = 0
show where the deleted records are equal to 0.
Here is what I have tried so far:
db.rental.find({"Autos.deleted":{$ne: 1}}).pretty()
db.rental.find({"Autos": {$elemMatch: {deleted: 1 } } } ).pretty()
db.rental.find({"Autos.deleted": 0},{"Autos": {$elemMatch:
{deleted:0}}});
But none of the above work for me. What am I doing wrong?
Desired output:
{
"_id" : ObjectId("5c7e459f875ea5548de25722"),
"Autos" : [
{
"_id" : ObjectId("5cad9a8be1c3895999adacef"),
"deleted" : 0,
},
{
"_id" : ObjectId("5cad9aa4e1c3895999adacf0"),
"deleted" : 0,
}
]
}
{
"_id" : ObjectId("5c7e45e9875ea5548de25724"),
"Shoemaking" : [
{
"_id" : ObjectId("5cad9a5de1c3895999adaced"),
"deleted" : 0,
},
]
}
I want the output to be something like the above, but all the queries I have tried so far either select only one record of Array or select nothing at all.
db.rental.aggregate([
{
$project: {
Autos: {
$filter: {
input: "$Autos",
as: "auto",
cond: { $eq:["$$auto.deleted",0] }
}
}
}
}
])
db.rental.find({ "Autos.deleted": { $eq: 0 } } )
use $elemMatch to match contain inside array. mongodb-$elemMatch
db.rental.find({
Autos: {
$elemMatch: {
deleted: 0
}
}
})
Code created in Mongoose to update a subdocument was not working. So I tried to update the subdocument within the Mongo Shell.
This is the document (location) and subdocument (review):
{
"_id" : ObjectId("56d8c73314fbc7e702cfb8c4"),
"name" : "Costly",
"address" : "150, Super Street",
"coords" : [
-0.9630884,
51.451041
],
"reviews" : [
{
"author" : "kyle riggen1",
"_id" : ObjectId("56d8de74cc7f953efd8455d9"),
"rating" : 4,
"timestamp" : ISODate("2015-06-01T06:00:00Z"),
"reviewText" : "will the ID work?"
}
],
"rating" : 0,
"__v" : 2
}
Here are some of my attempts at updating the subdocument:
This question gave this format:
update({
_id: "56d8c73314fbc7e702cfb8c4",
"reviews._id": ObjectId("56d8de74cc7f953efd8455d9")
},{
$set: {"reviews.$.rating": 1}
}, false, true
);
This returned an error of "update is not defined" as shown:
2016-03-03T22:52:44.445-0700 E QUERY [thread1] ReferenceError: update is not defined :
#(shell):1:1
Which I think is because the command did not start with db.locations.update()
MongoDB documentation used this format:
db.locations.update(
{
_id: "56d8c73314fbc7e702cfb8c4",
review: { $elemMatch: { author: "kyle riggen1" } }
},
{ $set: { "location.$.rating" : 1 } }
)
This returns a valid update but the update didn't actually happen as shown:
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
This question used this format:
db.locations.update({
_id: "56d8c73314fbc7e702cfb8c4",
'review.author': 'kyle riggen1'
},
{ $set: { 'review.$.rating': 1 }}
)
This returns the same as the MongoDB documentation as shown here:
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
So these queries I guess are working but my data is not getting updated. Would my data be indexed wrong perhaps? The actual location is able to be updated even through a Mongoose API.
You can do it By $push Or $addToSet.
db.col.update(
{ name: 'reviews', 'list.id': 2 },
{$push: {'list.$.items': {id: 5, name: 'item5'}}}
)
See the reference from mongodb Manual
https://docs.mongodb.org/manual/reference/operator/update/push/
Please know db.collection.update( criteria, objNew, upsert, multi )
criteria: match condition
objNew: update content
upsert: true or false
true : if not existed, insert it
false : if not existed, don't insert
multi: true or false
true : update all matched documents
false : only update the first matched document
I have a schema :
{
"id": String,
"username": String,
"password": String,
"email": String,
"firstName": String,
"lastName": String,
"system" : {
"item" : {type: Number},
"update_interval" : { type: Number, max: 99999 },
"reading" : [
{
"id" : { type: Number},
"adc1" : { type: Number, max: 4095 },
"adc2" : { type: Number, max: 4095 },
"pc_datestamp" :Date,
}
]
}
now i want to add values to
"reading" : [
{
"id" : { type: Number},
"adc1" : { type: Number, max: 4095 },
"adc2" : { type: Number, max: 4095 },
"pc_datestamp" :Date,
}
]
but i dont know where I am wrong I have tried to update data from mongoshell but no success till now
> db.users.update( {"email" : "test#test.com", "system.item": 1, }, {"$push": {"system.$.reading": [{"adc1" : "123", "adc2": "1245", "id":"1" }] } })
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: system.$.reading"
}
> db.users.update( {"email" : "test#test.com", "system": {$elemMatch:{ item: 1}} }, {"$push": {"system.$.reading": {"adc1" : "123", "adc2": "1245", "id":"1" } } })
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
I have set the value of item as one
> db.users.find( {"email" : "test#test.com", "system.item": 1} ).pretty()
{
"_id" : ObjectId("56dd88578ff7fbd714091a4a"),
"lastName" : "test",
"firstName" : "test",
"email" : "test#test.com",
"password" : "$2a$10$wY9wr9oreza4fBX7CfXym.rPZUPrcesigYIfWd0zbM4dDjBy6k3vy",
"username" : "test",
"system" : {
"item" : 1,
"reading" : [ ]
},
"__v" : 0
}
I have followed
Mongodb $push in nested array
and this
Insert data in nested array in mongodb
and many more questions but cannot find whats wrong.
Since the positional $ operator acts as a placeholder for the first element that matches the query document, and the array field must appear as part of the query document, your update
operation does not satisfy these conditions hence it suffers from the error fate you are getting. In your query you are only referencing the "system.item" field which is not an array.
The other option you can do is ditch the positional $ operator in your update and just add the object to the array using $addToset which adds elements to an array only if they do not already exist in the set:
db.users.update(
{
"email": "test#test.com",
"system.item": 1
},
{
"$addToSet": {
"system.reading": {
"adc1" : "123",
"adc2": "1245",
"id":"1"
}
}
}
)
So i was trying to update data store in the mongodb database, using Mongoose
original data structure looks like this
{
"_id" : ObjectId("234u232kjrkjwebrkw"),
"local" : {
"password" : "sdflsdjflsdjlfkjsdlkfjklsdjflksd",
"email" : "email#email.com"
},
"__v" : 0
}
I'm trying to update the "userName" property in the "local". after which it supposed to be
{
"_id" : ObjectId("234u232kjrkjwebrkw"),
"local" : {
"password" : "sdflsdjflsdjlfkjsdlkfjklsdjflksd",
"email" : "email#email.com",
"userName" : "yowhatsup"
},
"__v" : 0
}
I used this
User.findByIdAndUpdate("54a490ab6e13cca1d47870d6", {local:{ userName: 'jasonBorne' }}, { upsert: true }, function(){})
it became this
{
"_id" : ObjectId("234u232kjrkjwebrkw"),
"local" : {
"userName" : "yowhatsup"
},
"__v" : 0
}
it's been overwritten.
how to avoid this?
note: the new "userName" is defined as {type:String,default:null} when the model was structred.
You can do an an update if exists, otherwise insert:
Arguments for update are: findQuery, data, queryOptions, onComplete
User.update({"_id" : ObjectId("234u232kjrkjwebrkw")}, { $set: { "local.userName": "whatsup" } }, { upsert: true }, function(err){...});