Mongo save data if some fields not matched
up vote
0
down vote
favorite
I want to save data in mongo collection if data already not present, that can be checked by some fields.
I think I can't use mongo save()
or insert()
so I moved to mongo update()
with upsert: true
.
the query that is fired is :
const query = {
$and: [
{ invitationTo: { $ne: some.invitationTo } },
{ invitationBy: { $ne: some.invitationBy } },
{ listingId: { $ne: some.listingId } }
]
}
const options = {
upsert: true
}
db.contactRequests.update(query, contactRequestsVal, options)
I am not having a luck here. Suggest me the proper way of doing this if possible in a single query.
The collection contactRequests contains:
invitationTo: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
invitationBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
listingId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'listings'
},
message: {
type: String,
required: true
},
attachment: {
type: String
}
mongodb mongoose nosql mongodb-query
add a comment |
up vote
0
down vote
favorite
I want to save data in mongo collection if data already not present, that can be checked by some fields.
I think I can't use mongo save()
or insert()
so I moved to mongo update()
with upsert: true
.
the query that is fired is :
const query = {
$and: [
{ invitationTo: { $ne: some.invitationTo } },
{ invitationBy: { $ne: some.invitationBy } },
{ listingId: { $ne: some.listingId } }
]
}
const options = {
upsert: true
}
db.contactRequests.update(query, contactRequestsVal, options)
I am not having a luck here. Suggest me the proper way of doing this if possible in a single query.
The collection contactRequests contains:
invitationTo: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
invitationBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
listingId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'listings'
},
message: {
type: String,
required: true
},
attachment: {
type: String
}
mongodb mongoose nosql mongodb-query
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to save data in mongo collection if data already not present, that can be checked by some fields.
I think I can't use mongo save()
or insert()
so I moved to mongo update()
with upsert: true
.
the query that is fired is :
const query = {
$and: [
{ invitationTo: { $ne: some.invitationTo } },
{ invitationBy: { $ne: some.invitationBy } },
{ listingId: { $ne: some.listingId } }
]
}
const options = {
upsert: true
}
db.contactRequests.update(query, contactRequestsVal, options)
I am not having a luck here. Suggest me the proper way of doing this if possible in a single query.
The collection contactRequests contains:
invitationTo: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
invitationBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
listingId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'listings'
},
message: {
type: String,
required: true
},
attachment: {
type: String
}
mongodb mongoose nosql mongodb-query
I want to save data in mongo collection if data already not present, that can be checked by some fields.
I think I can't use mongo save()
or insert()
so I moved to mongo update()
with upsert: true
.
the query that is fired is :
const query = {
$and: [
{ invitationTo: { $ne: some.invitationTo } },
{ invitationBy: { $ne: some.invitationBy } },
{ listingId: { $ne: some.listingId } }
]
}
const options = {
upsert: true
}
db.contactRequests.update(query, contactRequestsVal, options)
I am not having a luck here. Suggest me the proper way of doing this if possible in a single query.
The collection contactRequests contains:
invitationTo: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
invitationBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
listingId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'listings'
},
message: {
type: String,
required: true
},
attachment: {
type: String
}
mongodb mongoose nosql mongodb-query
mongodb mongoose nosql mongodb-query
edited Nov 8 at 13:35
asked Nov 8 at 13:30
Sandeep
198
198
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208764%2fmongo-save-data-if-some-fields-not-matched%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown